Vue3 watch 侦听 props 的变化

写法一:
// 侦听一个 getter
const state = reactive({
    
     count: 0 })
watch(
  () => state.count,
  (count, prevCount) => {
    
    
    /* ... */
  }
)
 
// 直接侦听一个 ref
const count = ref(0)
watch(count, (count, prevCount) => {
    
    
  /* ... */
})


写法二:
// 假设 props 上有个 name 属性
// 下面的写法会生效
watch(
  () => props.name,
  (count, prevCount) => {
    
    
    /* ... */
  }
)
 
// 下面的写法不会被触发
watch(props.name, (count, prevCount) => {
    
    
  /* ... */
})

猜你喜欢

转载自blog.csdn.net/weixin_45932157/article/details/125888493