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

原代码如下:

javascript 复制代码
<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方法删除数组

javascript 复制代码
function removeItem(item) {
  const index = historyAccount.findIndex(ele => ele.phone == item.phone)
  if (index !== -1) {
    historyAccount.splice(index, 1)
  }
  console.log(historyAccount)
}
相关推荐
橙序员小站2 小时前
Agent Skill 是什么?一文讲透 Agent Skill 的设计与实现
前端·后端
炫饭第一名4 小时前
速通Canvas指北🦮——基础入门篇
前端·javascript·程序员
王晓枫5 小时前
flutter接入三方库运行报错:Error running pod install
前端·flutter
符方昊5 小时前
React 19 对比 React 16 新特性解析
前端·react.js
ssshooter5 小时前
又被 Safari 差异坑了:textContent 拿到的值居然没换行?
前端
曲折5 小时前
Cesium-气象要素PNG色斑图叠加
前端·cesium
Forever7_5 小时前
Electron 淘汰!新的桌面端框架 更强大、更轻量化
前端·vue.js
Angelial5 小时前
Vue3 嵌套路由 KeepAlive:动态缓存与反向配置方案
前端·vue.js
jiayu6 小时前
Angular学习笔记24:Angular 响应式表单 FormArray 与 FormGroup 相互嵌套
前端
jiayu6 小时前
Angular6学习笔记13:HTTP(3)
前端