我已经基于github https://github.com/robinvdvleuten/vuex-persistedstate设置了createdPersistedState
插件/localStorage.js
import createPersistedState from 'vuex-persistedstate'
export default ({store}) => {
createPersistedState({
key: 'vuex',
paths: ['local']
})(store)
}
nuxt.config.js
plugins:[
{ src: '~/plugins/localStorage.js', ssr: false }
],
中间件/auth.js
import axios from 'axios'
export default function ({ store, params }) {
return axios.post( process.env.baseApi + '/login', {
email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="35504d545845595007755f40545b5850475e54515a1b565a58" rel="noreferrer noopener nofollow">[email protected]</a>',
password: '12345678'
})
.then(response => {
//How to save it in createPersistedState or How can I do it here?
store.dispatch('token', response.data.token)
})
}
如何使用 middleware/auth.js 中的createdPersistedSate将其保存在localStorage中?
最佳答案
import createPersistedState from 'vuex-persistedstate'
export default ({store}) => {
createPersistedState({
...
})(store)
}
删除这个即可
key: 'vuex',
paths: ['local']
关于Nuxt.js 使用中间件 createPersistedState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51355442/