实现需求如下图所示:
当添加的内容输入后,没有保存,切换了页面路由,再返回到此页面,数据内容消失了;需要保存输入的内容处理;
方法一、通过添加缓存方法添加保存;以下是部分主要代码:
主要本地存储代码
// 取缓存
getStorage() {
this.newVal = JSON.parse(localStorage.getItem('alloatAddInfo')) ? JSON.parse(localStorage.getItem('alloatAddInfo')) : {}
// 根据需求赋值
this.basic = this.newVal
this.buyerName = this.newVal.buyerName ? this.newVal.buyerName : ''
this.tableData = this.newVal.itemInfo && this.newVal.itemInfo.length > 0 ? this.newVal.itemInfo : [{
id: 0,
type: 'custom'
}]
},
// 监听添加缓存
watchStorage(data) {
const params = {
...data
}
localStorage.setItem('alloatAddInfo', JSON.stringify(params))
},
以上内容仅供参考!