vue 处理数据变化但页面不实时刷新问题

问题描述:
我的table表格里有一个下拉框,在层级比较多的情况下给下拉赋值,但是页面未能实时刷新,我左思又想,左查右看,,急的电脑都要砸了但是还是出不来时。恰巧你刷到了我的这条博客,,那么恭喜你  你的电脑性命保住了          
第一个方案,当然是使用官方提供的方法啦,假如格式是下面这样
data() {
  return() {
    groupslist: {
      groupId: '01111111113',
      name: '张三'
    }
  }
}
赋值 let newId = '2333332412' this.$set(this.groupslist, 'groupId', newId) 如果页面数据刷新了则皆大欢喜,不好还得骂骂咧咧(狗头)
第二个方案
简单粗暴:
this.groupslist.groupslist = newId
this.$forceUpdate() // 强制刷新
究极解决方案:深拷贝数组,然后重新赋值
深拷贝方法
cloneCopy(target) {
    let result = Array.isArray(target) ? [] : {}
    if (target && typeof target === 'object') {
        for (let key in target) {
            if (typeof target[key] === 'object') {
                result[key] = cloneCopy(target[key])
            } else {
                result[key] = target[key]
            }
        }
        return result
    }
    return ''
}

赋值
let newArr = this.cloneCopy(this.groupslist)
然后你操作newArr ,比如循环赋值啊啥的 ,按你的功能需求操作
最后,this.groupslist = newArr 

这样就结束了,你会说  我操(狗头)

那种有用就用那种,希望你我都没烦恼

posted @ 2021-09-13 18:34  安雁-zzq  阅读(2368)  评论(0编辑  收藏  举报