Vue 數(shù)值改變頁面沒有刷新的問題解決(數(shù)據(jù)改變視圖不更新的問題)
解決數(shù)據(jù)改變視圖不更新的問題
情況描述
值(List)修改后,打印值改變了,但頁面數(shù)據(jù)沒刷新
- 如何賦值:
this.tableData[index] = { ...this.tableData[index], ...result } - 想法:利用解構賦值,修改對應key的value值
- 例:this.tableData[index] = {id:1,name:jarry} result={id:2}
原因
解構賦值如果所解構的原對象是一維數(shù)組或?qū)ο?,其本質(zhì)就是對基本數(shù)據(jù)類型進行等號賦值,那它就是深拷貝;
如果是多維數(shù)組或?qū)ο螅浔举|(zhì)就是對引用類型數(shù)據(jù)進項等號賦值,那它就是淺拷貝;
最終的結論就是:解構賦值是淺拷貝(因為它確實不能對多維數(shù)組或?qū)ο筮_到深拷貝的作用);
解決方案
1.直接賦值
Object.keys(result).forEach(item => {
this.tableData[index][item] = result[item];
})2.$set
Object.keys(result).forEach(item => {
this.$set(this.tableData[index], item, result[item])
})3.重新賦值 JSON.parse(JSON.stringify(this.tableData))
this.tableData[index] = { ...this.tableData[index], ...result };
this.tableData = JSON.parse(JSON.stringify(this.tableData))4.v-if&&this.$nextTick 重新渲染頁面
<Table :columns="columns" :data="tableData" v-if="isTable"></Table>
this.$nextTick(()=>{
this.isTable = true;
})當表格數(shù)據(jù)改變,表格沒刷新時,并且使用了render
例如:如下是增加了表格可展開行,但是 展開行在重新賦值時并沒有更新頁面

columns: [
{
type: "expand",
width: 30,
align: "center",
render: (h, params) => {
return h(
"div",
{
style: {
padding: "5px 0 5px 30px",
backgroundColor: "#fff",
},
},
[
h("Table", {
props: {
border: true,
columns: this.columns1,
data: this.data[params.index].children || [],
tablekey: this.tablekey,
},
}),
]
);
},
}
]解決方案
添加 tablekey ,當賦值以后, this.tablekey++

this.data.forEach((item) => {
if (item.id === id) {
let result = res.result || [];
item.children = result;
this.submitBindData.ids = result.map((o) => o.id).join(",");
}
});
++this.tablekey;到此這篇關于Vue 數(shù)值改變頁面沒有刷新的問題解決(解決數(shù)據(jù)改變視圖不更新的問題)的文章就介紹到這了,更多相關vue數(shù)據(jù)改變視圖不更新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
手寫Vue內(nèi)置組件component的實現(xiàn)示例
本文主要介紹了手寫Vue內(nèi)置組件component的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07

