Vue 中 watch 不会触发的情况

新手上路,请多包涵

父组件:

 <div class="table-item" v-for="(item, index) in MonitorHost" :key='index'>
  <hostTable :host='item'></hostTable>
   </div>

子组件:

  props: {
        host: {
            type: String
        }
    },
  watch: {
        async host(val) {
            console.log(val);
            let res = await this.findTask({...this.Form, host: val});
            if (res && res.rltCode === 200) {
                this.Form.Total = res.data.totalCount;
                this.tableData = res.data.data;
            }
        }
    },

发现watch根本就没有触发,如果不循环的话,就可以触发,这是什么原因

阅读 5k
3 个回答
 host: {
    async handler(val) {
      // ...
    },
    immediate: true
  }

在初始化的时候 watch 不会被触发,默认是第二次值改变的时候触发,需要第一次触发添加属性:

immediate: true
  watch: {
        host:{
            handler: function (val) {
                console.log(val);
            },
        deep: true
       }
  },
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题