首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何监控子组件中父组件的数据变化

问如何监控子组件中父组件的数据变化
EN

Stack Overflow用户
提问于 2021-05-21 16:37:42
回答 2查看 50关注 0票数 0

当父组件的数据发生变化时,如何在子组件中实现实时监控和响应。

EN

回答 2

Stack Overflow用户

发布于 2021-05-21 16:50:25

代码语言:javascript
复制
props: ["opType", "editData"],
watch: {
  editData: {
    immediate: true,
    handler(newVal, oldVal) {
      if (this.editData.pryType === "MOBN") {
        this.infoIndex = 0;
      } else if (this.editData.pryType === "EMAL") {
        this.infoIndex = 1;
      } else if (this.editData.pryType === "SVID") {
        this.infoIndex = 2;
      } else if (this.editData.pryType === "BBIN") {
        this.infoIndex = 3;
        if (this.getListSeconed) {
          this.getkList();
          this.getListSeconed = false;
        }
      }
    },
    deep: true
  }
},

可以试着这样做。

票数 0
EN

Stack Overflow用户

发布于 2021-05-21 17:37:34

当道具发生变化时,您可以观看道具:

代码语言:javascript
复制
<div id="app">
  <childcomponent :myprop="text"></childcomponent>
  <button @click="text = 'New text'">Change text</button>
</div>

使用Watch方法监视ChildComponent中的更改!

代码语言:javascript
复制
new Vue({
  el: '#app',
  data: {
    text: 'This is Text'
  },
  components: {
    'childcomponent' : {
     template: `<p>{{ myprop }}</p>`,
     props: ['myprop'],
     watch: { 
      myprop: function(newValue, oldValue) { 
       console.log('Prop changed and new value is: ', newVal, ' | old value: ', oldValue);
        }
      }
    }
  }
});

https://vuejs.org/v2/api/#watch

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67633440

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档