typescript如何使用watch?

如何将下面的js版的watch,改成ts版:

watch: {
    checkVal: {
      handler: function (val) {
        if (val) {
          this.checkList.forEach((item) => {
            this.checkVal.forEach((i) => {
              if (i == item[this.idKey]) {
                this.checkRightList.push(item);
              }
            });
          });
          const res = new Map();
          this.checkRightList = this.checkRightList.filter(
            (a) => !res.has(a[this.idKey]) && res.set(a[this.idKey], 1)
          );
        }
      },
      deep: true,
    },

    showList: {
      handler: function (val) {
        if (val) {
          this.$emit('change', val);
        }
      },
      deep: true,
    },
  },
阅读 1.7k
1 个回答

可以看看这篇文章: https://github.com/kaorun343/vue-property-decorator

import { Vue, Component, Watch } from 'vue-property-decorator'

@Component
export default class YourComponent extends Vue {
  @Watch('child')
  onChildChanged(val: string, oldVal: string) {}

  @Watch('person', { immediate: true, deep: true })
  onPersonChanged1(val: Person, oldVal: Person) {}

  @Watch('person')
  onPersonChanged2(val: Person, oldVal: Person) {}

  @Watch('person')
  @Watch('child')
  onPersonAndChildChanged() {}
}
推荐问题
logo
Microsoft
子站问答
访问
宣传栏