Vue2.0,用 watch 观察 prop 变化,但没有触发

2016-11-24 19:03:03 +08:00
 akaylh

A 组件:

export default {
  props:{
  	name:{
  		type:String
  	}
  },
  data () {
    return {
      author: "Jinkey"
    }
  },
  mounted:function(){
  	this.author = 'lili'
  },
  watch:{
  	name:function(){
  		console.log(this.name);
  	},
  	author:function(){
  		console.log('lili');
  	}
  }
}

author 有监测到变化,并输出了 lili ; name 由 B 组件传入,却没有监测到,控制台没有输出。

在 B 组件里调用 A 组件,并传值给 name

<firstcomponent :name="name"></firstcomponent>

import firstcomponent from './component/firstcomponent.vue'
export default {
  data () {
    return {
      msg: 'Hello Vue!',
      name:'lili'
    }
  },
  components: { firstcomponent}
}
27573 次点击
所在节点    Vue.js
3 条回复
akaylh
2016-11-24 21:18:39 +08:00
测试了下,发现 watch 好像对 String 类型的 prop 无效,但对 Object 类型的 Prop 有效
sox
2016-11-24 21:45:41 +08:00
name 又没改变怎么会触发
akaylh
2016-11-25 08:19:01 +08:00
@sox 根据您的提示,仔细想了是,是我把概念搞错了,已经可以了,非常感谢。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/323053

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX