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,
};
},
});