vue3 reactive包裹数组无法页面无法响应式

原代码如下:

<div class="section" v-for="(item, i) in historyAccount" :key="i" v-show="item.flag">
   <span v-html="changeColor(item)"></span>
   <img src="@/assets/images/login/clearUserName3x.svg" @click="removeItem(item)" />
 </div>
//历史登录账号数据
let historyAccount = reactive([
  {
    phone: '18896722354',
    flag: false
  },
  {
    phone: '15056678907',
    flag: false
  }
])

//这里用filter方法删除所循环的historyAccount的值
function removeItem(item) {
  console.log(item)
  historyAccount = historyAccount.filter(ele => ele.phone !== item.phone)
  console.log(historyAccount)
}

此时removeItem方法运行,打印出historyAccount的值确实被改变了,但是页面还是没有变化

原因:

如果你直接通过赋值的方式改变reactive对象引用的数组,是不会触发视图的更新的,应该使用 Vue 提供的响应式方法来更新响应式数据。

 改进:可以利用splice方法删除数组

function removeItem(item) {
  const index = historyAccount.findIndex(ele => ele.phone == item.phone)
  if (index !== -1) {
    historyAccount.splice(index, 1)
  }
  console.log(historyAccount)
}

相关推荐

  1. vue3 reactive包裹数组无法页面无法响应

    2024-04-09 19:10:02       35 阅读
  2. Vue3:ref和reactive实现响应数据

    2024-04-09 19:10:02       45 阅读
  3. Vue3reactive对象类型的响应数据

    2024-04-09 19:10:02       24 阅读
  4. 第16节:Vue3 响应对象reactive()

    2024-04-09 19:10:02       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-09 19:10:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-09 19:10:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-09 19:10:02       87 阅读
  4. Python语言-面向对象

    2024-04-09 19:10:02       96 阅读

热门阅读

  1. 定制您的设备体验:如何更改Android启动动画

    2024-04-09 19:10:02       35 阅读
  2. 出海的网络挑战

    2024-04-09 19:10:02       37 阅读
  3. ALTER TABLE 之 慢速变更(slow alter)

    2024-04-09 19:10:02       38 阅读
  4. LeetCode 670. 最大交换

    2024-04-09 19:10:02       37 阅读
  5. memset()函数及其作用

    2024-04-09 19:10:02       37 阅读
  6. 蓝桥杯-【二分】求阶乘

    2024-04-09 19:10:02       40 阅读
  7. 力扣经典150题第九题:跳跃游戏

    2024-04-09 19:10:02       36 阅读
  8. K8S Deployment HA

    2024-04-09 19:10:02       29 阅读
  9. 查找同一个进程显示在任务栏上的多个窗口

    2024-04-09 19:10:02       36 阅读