vue3 reactive使用(四)

1:reactive全家桶

rective直接赋值无效,会破坏proxy响应式代理

const newList = reactive<string[]>([])
const newCreate = () => {
  setTimeout(() => {
    const res = ['生活', '升华', '生生不息']
    newList = res  
    console.log('newList:', newList)  // dom没有变化,log会有变化
  }, 500)
}

变通

方案一
const newCreate = () => {
  setTimeout(() => {
    const res = ['生活', '升华', '生生不息']
    newList.push(...res)  // 使用push方法,和解构语法
    console.log('newList:', newList) // dom双向绑定成功,log也正常
  }, 500)
}
方案二

添加一个对象,把数组作为一个属性去解决

let newList = reactive<{
  array: string[]
}>({
  array: [],
})

const newCreate = () => {
  setTimeout(() => {
    const res = ['生活', '升华', '生生不息']
    newList.array = res  // 直接赋值,可以生效
    console.log('newList:', newList)  // dom和log正常显示
  }, 500)
}
  1. readonly:只读
  2. shallowReactive和shallowRef类似,做浅层处理
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值