data.searchResult 是表格的数据
function detailsDataSave(){
const rowObj ={}
// 通过判断有没有data.detailsId,有没有来选择 post 或 patch 方法
if(data.detailsId){
rowObj.id= data.detailsId
rowObj.is_active = true
// 修改保存数据
patchMethod(modData.url + rowObj.id, rowObj)
// 页面更新
data.searchResult = data.searchResult.map(item=>{ // 这里可以更新页面,之前页面本身有数据 即data.searchResult 数组不为空
if(item.id===rowObj.id){
return rowObj
} else {return item}
})
} else {
rowObj.id = nanoid()
rowObj.is_active = true
// 新增保存数据
postMethod(modData.url, rowObj)
// 页面更新
data.searchResult.push(rowObj) // 这里不能更新页面,之前页面本身没数据, 即data.searchResult 数组length 为0
// data.searchResult.splice(0,0, rowObj) // 用这个方法也不更新
console.log ('data.searchResult2',data.searchResult)
}
}
请教!
方法一、this.$set
方法二、在表格上加:key,key值要动态变化,更新数据后更新key值
两种方法都能解决,优先使用this.$set
data.searchResult=[...data.searchResult,rowObj]这样更新看看!
console.log打印出来的是空数组吗?
data.searchResult = []
data.searchResult.push(rowObj)
要先定义数组才能push
可以采用watch进行路由监听进行页面刷新
借用代码
mounted() {
this.getPath()
},
methods: {
getPath(){
console.log(this.$route.path);
if (this.$route.path == '你要进入的路由') {
this.init() // 初始化的方法
}
}
},
watch: {
'$route':'getPath'
},
可以用 this.$set('data.searchResult',rowObj)