vue composition-api tips

2020-04-24 18:40:34 浏览数 (1)

Watch

  • watch 监听 props 属性时,不能使用解构赋值
代码语言:javascript复制
// 解构
 setup({ notifie }){
        
        const localNotifie = ref({})
        const [ set, { add, remove } ] = useSet(localNotifie.value.target)

        // notifie  变化时, 无法触发监听
        watch( () => notifie, () => {
            console.log('watch')
            localNotifie.value = cloneDeep(notifie)
        })
      
        return {
            ....
        }
    }



// 非结构
setup(props){
        
        const localNotifie = ref({})
        const [ set, { add, remove } ] = useSet(localNotifie.value.target)
        // notifie  变化时, 触发监听
        watch( () => props.notifie, () => {
            console.log('watch')
            localNotifie.value = cloneDeep(props.notifie)
        })
      
        return {
            ....
        }
    }

0 人点赞