【vue】vue3怎么使用watch监听props传入的数组的变化
watch函数接受三个参数:
- 一个想要侦听的响应式引用或 getter 函数
- 一个回调
- 可选的配置选项
// 子组件
import { defineComponent, watch } from 'vue';
export default defineComponent({
name: 'test',
props: {
dataList: {
type: Array,
},
},
setup(props) {
watch(
() => props.dataList as [],
(newList, oldList) => {
// 监听props.dataList的变化,每次变化都执行init方法
init();
},
{ deep: true }
);
function init() {}
return {
init,
};
},
});