JavaScript/vue.js/store:修订间差异
来自康健生活
创建页面,内容为“== 引言 == 留白 == store更新== === 视图未更新 === 今天做上传组件遇到一个问题,store 值更新后,组件中的 store 并没有更新。…” |
|||
| (未显示同一用户的12个中间版本) | |||
| 第1行: | 第1行: | ||
{{Sign}} | |||
== 引言 == | == 引言 == | ||
留白 | 留白 | ||
| 第10行: | 第11行: | ||
流程图如下 | 流程图如下 | ||
[[文件:Vue new store 1.png|600px|无|none|Vue new store | [[文件:Vue new store 1.png|700px|无|none|Vue new store 1]] | ||
现在的问题是上传结束后,store 更新后,组件中的 store 值依然是上次的值(等于视图组件值没有更新)。 | |||
<s>以下是尝试后的失败的方法</s> | |||
<syntaxhighlight lang="javascript"> | |||
data () { | |||
return { | |||
sessionid: this.$store.state.file.sessionid | |||
}; | |||
}, | |||
computed: { | |||
watchSid () { | |||
// 需要监听的数据 | |||
return this.$store.state.file.sessionid | |||
} | |||
}, | |||
watch:{ | |||
watchSid:{ | |||
handler (val){ | |||
this.sessionid = val | |||
}, | |||
deep:true, | |||
immediate:true | |||
} | |||
},</syntaxhighlight> | |||
我试图通过监听 store 的值变化来更新组件的 data(通过 data 中转并缓存 sessionId),并没有管用。 | |||
所以换一个思路,或许不是取值过程中出现错误,而是其他地方导致问题,先查看store 文件 | |||
{{hc|store/file.js|<syntaxhighlight lang="javascript"> | |||
/** | |||
* TODO 用户相关的 store | |||
* @author kangkk | |||
*/ | |||
const __init__ = { | |||
sid: 99999, | |||
id: 99999, | |||
docFrom: 'orig', | |||
agreement: false, | |||
bid: '0', | |||
category: 'cs', | |||
content: '', | |||
title: '', | |||
fileList: [], | |||
photoList: [] | |||
} | |||
export const state = () => { | |||
return Object.assign({}, __init__) | |||
} | |||
const actions = { | |||
initFile ({ commit }) { | |||
commit('initFile') | |||
}, | |||
setFile ({ commit }, fileObj) { | |||
commit('updateFile', fileObj) | |||
}, | |||
deleteFile ({ commit }, url) { | |||
commit('deleteFile', url) | |||
}, | |||
updateFileList ({ commit }, fileList) { | |||
commit('updateFile', { | |||
fileList: fileList | |||
}) | |||
}, | |||
updatePhotoList ({ commit }, photoList) { | |||
commit('updateFile', { | |||
photoList: photoList | |||
}) | |||
}, | |||
addFile ({ commit }, fileItem) { | |||
commit('addFile', fileItem) | |||
}, | |||
addPhoto ({ commit }, photoItem) { | |||
commit('addPhoto', photoItem) | |||
}, | |||
} | |||
const mutations = { | |||
initFile (state) { | |||
Object.assign(state, __init__) | |||
}, | |||
updateFile (state, fileObj) { | |||
Object.assign(state, fileObj) | |||
}, | |||
addFile (state, fileItem) { | |||
const newFileList = state.fileList.push(fileItem) | |||
Object.assign(state, { | |||
fileList: newFileList | |||
}) | |||
}, | |||
addPhoto (state, photoItem) { | |||
const newPhotoList = state.photoList.push(photoItem) | |||
Object.assign(state, { | |||
photoList: newPhotoList | |||
}) | |||
}, | |||
deleteFile (state, url) { | |||
const deteleIndex = state.fileList.findIndex((o) => { | |||
const tmpUrl = o['url'] || o['fileUrl']; | |||
if (tmpUrl === url) { | |||
return o; | |||
} | |||
}); | |||
state.fileList.splice(deteleIndex, 1); | |||
}, | |||
} | |||
export default { namespaced: true, state, mutations, actions } | |||
</syntaxhighlight>}} | |||
重新走一遍逻辑,截图如下 | |||
{| class="wikitable" | |||
|- | |||
! 上传过程中 sesion 缓存的值 !! 上传成功后刷新页面 session 缓存的值 !! 上传成功后页面中 store 的值 | |||
|- | |||
| [[文件:Vue new store 2.png|250px|none|无|Vue new store 2]] | |||
| [[文件:Vue new store 3.png|250px|无|none|Vue new store 3]] | |||
| [[文件:Vue new store 4.png|250px|无|Vue new store 4]] | |||
|} | |||
这里直接看到抛错的问题点在 session 缓存的值(9999 这个预设值,出现预设值说明已经抛错,即新的 sesionId -> sid 并没有存储到 store中),但是页面组件又显示 sessionId 已经更新,那么问题的节点已经找到。 | |||
为什么会出现 session 存储的值,这是由于 store 在浏览器页面渲染和服务器响应之间有一个时间差,会导致 store 延迟传递导致白屏(也许是白屏,也许是数据填充缓慢等),所以为了解决这个问题,在 plugins 中添加了组件向 session/cookies/localstorage 等中缓存 store | |||
{{hc|plugins/store.js|<syntaxhighlight lang="javascript"> | |||
/** | |||
* vuex 持久化 | |||
* @auther kangkk | |||
* @description 通过 localstorage 或者 cookie 或者 session 等将 vuex 的状态持久化,规避页面刷新后 vuex 数据丢失 | |||
*/ | |||
export default function (ctx){ | |||
// 离开页面 刷新前 将store中的数据存到session | |||
window.addEventListener('beforeunload', ()=> { | |||
sessionStorage.setItem("storeCache",JSON.stringify(ctx.store.state)) | |||
}); | |||
// 页面加载完成 将session中的store数据 | |||
window.addEventListener('load', ()=> { | |||
let storeCache = sessionStorage.getItem("storeCache") | |||
if(storeCache){ | |||
// 将session中的store数据替换到store中 | |||
ctx.store.replaceState(JSON.parse(storeCache)); | |||
} | |||
}); | |||
}</syntaxhighlight>}} | |||
流程如下: | |||
[[文件:Vue new store 5.png|600px|none|无|Vue new store 5]] | |||
可以感受到错位的地方在于我的store 中已经更新了值,但是页面组件取的 store 值是已经初始化,并且更新为从后端拿到新值并存入 store 后的值。简单说就是两个组件: | |||
* 子组件离开页面前时执行 <syntaxhighlight lang="javascript" inline> | |||
this.$store.dispatch('file/initFile');</syntaxhighlight> | |||
* 父组件加载新页面时执行 <syntaxhighlight lang="javascript" inline> | |||
store.dispatch('file/setFile', Object.assign({},SessionData))</syntaxhighlight>; | |||
我们期望的执行顺序是: | |||
[[文件:Vue new store 6.png|600px|none|无|Vue new store 6]] | |||
但实际情况却是这样: | |||
[[文件:Vue new store 7.png|600px|none|无|Vue new store 7]] | |||
实际上由于加载新页面时是使用 <code>async asyncData</code> 在服务端跑接口,导致 session 更新 store 与 asyncData 中接口更新 store 是一个并行竞争问题。所以抛错。 | |||
解决办法: | |||
将父组件的初始化 store 放到 mounted 中执行,并在此之前更新本地 session | |||
<syntaxhighlight lang="javascript"> | |||
mounted () { | |||
let storeCache = sessionStorage.getItem("storeCache") | |||
if(storeCache){ | |||
const tmp = JSON.parse(storeCache) | |||
if(tmp.file){ | |||
// 刷新 session 中缓存的 file 对象 | |||
sessionStorage.setItem("storeCache", JSON.stringify(Object.assign(tmp, { | |||
file: Object.assign(tmp.file, this.sessionData) | |||
}))) | |||
} | |||
} | |||
this.$store.dispatch('file/setFile', Object.assign({}, this.sessionData)); | |||
},</syntaxhighlight> | |||