V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
akaylh
V2EX  ›  Vue.js

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

  •  1
     
  •   akaylh · 2016-11-24 19:03:03 +08:00 · 27566 次点击
    这是一个创建于 2712 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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}
    }
    
    3 条回复    2016-11-25 08:19:01 +08:00
    akaylh
        1
    akaylh  
    OP
       2016-11-24 21:18:39 +08:00
    测试了下,发现 watch 好像对 String 类型的 prop 无效,但对 Object 类型的 Prop 有效
    sox
        2
    sox  
       2016-11-24 21:45:41 +08:00   ❤️ 1
    name 又没改变怎么会触发
    akaylh
        3
    akaylh  
    OP
       2016-11-25 08:19:01 +08:00
    @sox 根据您的提示,仔细想了是,是我把概念搞错了,已经可以了,非常感谢。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5543 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 08:05 · PVG 16:05 · LAX 01:05 · JFK 04:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.