欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Vue2.0用 watch 观察 prop 变化(不触发)

程序员文章站 2022-09-08 13:18:55
本文介绍了vue2.0用 watch 观察 prop 变化(不触发),分享给大家,具体如下: a 组件: export default { props:...

本文介绍了vue2.0用 watch 观察 prop 变化(不触发),分享给大家,具体如下:

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}
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。