store/index.js内置
export const state = () => ({
token:'',
userInfo: {},
})
export const mutations = {
setUserInfo(state, data) {
state.userInfo = data
state.token = data.access_token
},
exit(state){
state.userInfo = {}
state.token = ''
}
}
解决刷新vuex中数据丢失问题
// plugins/store-cache.js
export default function(ctx) {
window.addEventListener('beforeunload', () => {
localStorage.setItem("storeCache", JSON.stringify(ctx.store.state))
});
let storeCache = localStorage.getItem("storeCache")
if (storeCache) {
ctx.store.replaceState(JSON.parse(storeCache));
}
}
// nuxt.config.js
plugins: [
{src:'~/plugins/store-cache', ssr:false}
],
页面使用
import { mapState } from "vuex";
computed: {
...mapState(["token", "userInfo"]),
},
bug
[Vue warn]: The client-side rendered virtual DOM tree is not matching server-rendered content. This is likely caused by incorrect HTML markup, for example nesting block-level elements inside
, or missing . Bailing hydration and performing f
翻译:[Vue warn]:客户端呈现的虚拟DOM树与服务器呈现的内容不匹配。这可能是由于不正确的HTML标记造成的,例如在< p>中嵌套块级元素,或者缺少< tbody>
报错原因:服务端与客户端渲染的DOM结构不一样
解决方法
在组件最外层加上组件,该组件只是占位且用于仅在客户端渲染其他组件
|