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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM