【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,
    };
  },
});
posted @ 2022-03-29 14:13  leah-xx  阅读(6754)  评论(0编辑  收藏  举报