vue中的watch和$watch的用法实际场景和区别

mounted() {
        this.watch_WX_DialogVisible();
    },
    watch: {
        'basicInfoData.salesPlatform'(val) {
            this.detailBasicInfoForm[2][0].params.advanceLabel =
                '店铺:' + val;
            this.detailBasicInfoForm[2][0].params.advanceCascade.salesPlatform = val;
            this.clearPrintTemplate();
            if (val === 'ANNTO' && this.basicInfoData.platformCarrierCode === 'SF') {
                this.initPrinter();
            }
            this.$set(this.detailBasicInfoForm[2][0].params, 'advanceNotSearch', val === 'WX');
        },
       
    },
    methods: {
        // 监听微信打印模板事件
        watch_WX_DialogVisible() {
            this.$watch(
                function () {
                    return this.$refs.detailForm.$refs.advanceIns[1].$refs.advanceDialog.dialogVisible;
                },
                function (newval) {
                    if (newval && this.basicInfoData.salesPlatform === 'WX') {
                        this.$set(this.$refs.detailForm.$refs.advanceIns[1].$refs.advanceDialog.searchObj, 'keyword', this.basicInfoData.platformCarrierCode || '');
                        this.$refs.detailForm.$refs.advanceIns[1].$refs.advanceDialog.fetchData();
                    }
                }
}

1.Vue 中,$watch 是一个用于深度观察一个对象或者一个 Vue 实例属性的方法。

一旦被观察的对象发生改变,就会触发一个回调函数。

2,关于为什么 watch_WX_DialogVisible方法没有写在 watch: 里,而是使用 $watch?

主要是因为 Vue 的 watch 选项有一个限制:它只能监听 Vue 实例的数据变化。然而,dialogVisible 并不是 Vue 实例的数据,而是引用了一个子组件的属性。如果不想修改公共组件,使用 $watch 可以解决。 过度使用 $watch 可能会导致性能问题,,在这种情况下,你可能需要考虑其他解决方案,比如使用计算属性或者只对特定的数据使用 $watch。

3,watch_WX_DialogVisible这方法使用了 $watch,作用是监听 dialogVisible 属性。当这属性变为 true,且 salesPlatform 为 WX 时,设置 searchObj.keyword 为 platformCarrierCode 的值。

<template>
  <div>
    <div>我是组件</div>
    <el-button  @click="dataArrAdd()">向数组中添加元素</el-button>
    <div v-if="showMsg">添加成功</div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      dataArr: [],
      showMsg: false,
    };
  },
  //watch: {
  //  dataArr:function(newval,old) {
  //    this.showMsg= !this.showMsg;
 //   },
  },
  created() {
    let unDataArr = this.$watch(
      () => {
        return this.dataArr;
      },
      function(newval,old)  {
        this.showMsg= !this.showMsg;
         unDataArr();
      }
    );
  },
  methods: {
    dataArrAdd() {
      this.dataArr.push("1");
    },
  },
};

1,调用unDataArr()执行一次监听事件后就会移除监听

2,$watch中第一个参数是监听的对象,只要return的值发生变化,函数就会调用,第二个参数是function (newVal, oldVal) {}和watch的写法一样
3,$watch来监听之后来取消监听,不用占用性能

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小可爱的小飞云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值