V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
akaylh
V2EX  ›  Vue.js

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

  •  1
     
  •   akaylh · Nov 24, 2016 · 28245 views
    This topic created in 3448 days ago, the information mentioned may be changed or developed.

    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 replies    2016-11-25 08:19:01 +08:00
    akaylh
        1
    akaylh  
    OP
       Nov 24, 2016
    测试了下,发现 watch 好像对 String 类型的 prop 无效,但对 Object 类型的 Prop 有效
    sox
        2
    sox  
       Nov 24, 2016   ❤️ 1
    name 又没改变怎么会触发
    akaylh
        3
    akaylh  
    OP
       Nov 25, 2016
    @sox 根据您的提示,仔细想了是,是我把概念搞错了,已经可以了,非常感谢。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2670 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 04:00 · PVG 12:00 · LAX 21:00 · JFK 00:00
    ♥ Do have faith in what you're doing.