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,
},
},
回答:
可以看看这篇文章: 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() {}
}
以上是 typescript如何使用watch? 的全部内容, 来源链接: utcz.com/p/934203.html