vue數(shù)據(jù)更新了但在頁(yè)面上沒(méi)有顯示出來(lái)的解決方法
方法一:使用 this.$set() 進(jìn)行數(shù)據(jù)更新
this.$set(target, key, value);
target:要更改的數(shù)據(jù)源(可以是對(duì)象或者數(shù)組)
key:要更改的具體數(shù)據(jù)
value:重新賦的值
基本語(yǔ)法(對(duì)象):this.$set(要改變的對(duì)象,"要改變的對(duì)象屬性","新值")
基本語(yǔ)法(數(shù)組):this.$set(要修改的數(shù)組,要修改的數(shù)組下標(biāo),{ "要修改的數(shù)組對(duì)象,一個(gè)/多個(gè)" })
例如:
定義數(shù)據(jù)
// 對(duì)象 obj: { name: "小明", age: 18 } // 數(shù)組對(duì)象 arr: [{ name: "小王", age: 18 }, { name: "小張", age: 20 }] // 普通數(shù)組 twoArr: [11, 22, 33]
修改數(shù)據(jù)
// 對(duì)象 this.$set(this.obj, "name", "小劉") console.log(this.obj) // obj={ name: "小劉", age: 18 } // 數(shù)組對(duì)象 this.$set(this.arr, 1, { name: "小王", age: 19 }) console.log(this.arr) // arr=[{ name: "小王", age: 18 }, { name: "小王", age: 19 }] // 普通數(shù)組 this.$set(this.twoArr, 0, 99) console.log(this.twoArr) // twoArr=[99, 22, 33]
新增數(shù)據(jù)
// 對(duì)象 this.$set(this.obj, "sex", "男") console.log(this.obj) // obj={ name: "小明", age: 18, sex: "男" } // 數(shù)組對(duì)象 const len = this.arr.length this.$set(this.arr, len, { name: "小紫", age: 18 }) console.log(this.arr) // arr=[{ name: "小王", age: 18 }, { name: "小張", age: 20 }, { name: "小紫", age: 18 }] // 普通數(shù)組 const len2 = this.twoArr.length this.$set(this.twoArr, len2, 44) console.log(this.twoArr) // twoArr=[11, 22, 33, 44]
方法二:使用 this.$forceUpdate() 進(jìn)行強(qiáng)制更新
this.$forceUpdate() 強(qiáng)制更新方法,作用是觸發(fā) vue 的 update 方法
this.$forceUpdate(); // 強(qiáng)制更新
updateForm() { this.formData.name = "張三"; this.$forceUpdate(); // 強(qiáng)制更新 },
方法三:更改引用,創(chuàng)建一個(gè)新的數(shù)組或?qū)ο?,替換舊的數(shù)組或?qū)ο?,?qiáng)制更新視圖
// 用于數(shù)組 this.recordList = [...this.recordList] // 用于對(duì)象 this.recordObj = { ...this.recordObj }
在 el-table 表格中數(shù)據(jù)發(fā)生變化了,表格未重新渲染的問(wèn)題
解決辦法:
在 el-table 中添加一個(gè) key 值,設(shè)置成 Boolean 類型,在數(shù)據(jù)更新后去更新這個(gè) key 值
例子如下:
<el-table :key="refreshTable" :data="tableData" border> <el-table-column type="index" label="序號(hào)" width="120" align="center" /> <el-table-column prop="userName" label="姓名" align="center" /> <el-table-column label="操作" width="200" align="center"> <template slot-scope="scope"> <el-button type="text" @click="toUpdate(scope.row)">修改</el-button> <el-button type="text" @click="toDel(scope.row, scope.$index)">刪除</el-button> <el-button type="text" @click="toDetail(scope.row)">詳情</el-button> </template> </el-table-column> </el-table>
refreshTable: false, // 表格數(shù)據(jù)刷新標(biāo)志
this.refreshTable = !this.refreshTable // 重新渲染表格
在數(shù)據(jù)更新的后面加上 this.refreshTable = !this.refreshTable 即可
以上就是vue數(shù)據(jù)更新了但在頁(yè)面上沒(méi)有顯示出來(lái)的解決方法的詳細(xì)內(nèi)容,更多關(guān)于vue數(shù)據(jù)更新但頁(yè)面沒(méi)有顯示的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue+xlsx實(shí)現(xiàn)表格的導(dǎo)入導(dǎo)出功能
這篇文章主要介紹了vue+xlsx實(shí)現(xiàn)表格的導(dǎo)入導(dǎo)出功能,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-11-11vue-router 中router-view不能渲染的解決方法
本篇文章主要結(jié)合了vue-router 中router-view不能渲染的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05解決Vue控制臺(tái)報(bào)錯(cuò)Failed to mount component: tem
這篇文章主要介紹了解決Vue控制臺(tái)報(bào)錯(cuò)Failed to mount component: template or render function not defined.問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06詳解新手使用vue-router傳參時(shí)注意事項(xiàng)
這篇文章主要介紹了詳解新手使用vue-router傳參時(shí)注意事項(xiàng),詳細(xì)的介紹了幾種常見(jiàn)錯(cuò)誤,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06el-menu如何根據(jù)多層樹(shù)形結(jié)構(gòu)遞歸遍歷展示菜單欄
這篇文章主要介紹了el-menu根據(jù)多層樹(shù)形結(jié)構(gòu)遞歸遍歷展示菜單欄,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-07-07