【发布时间】:2018-02-16 07:57:10
【问题描述】:
我将我的应用程序分成server 和client 一个。我正在运行服务器并在 express 中使用 nuxt.render 中间件,我想在 nuxt 存储中获取我的用户/会话/任何内容。我制作了一个store/auth.js 文件:
export const state = () => ({
user: null
})
export const mutations = {
SET_USER: (state, user) => {
state.user = user
}
}
export const actions = {
nuxtServerInit ({commit}, {req}) {
console.log(req)
},
async login ({commit}, {username, password}) {
try {
const data = this.$axios.$post('/login', {username, password})
commit('SET_USER', data)
} catch (err) {
throw err
}
},
async logout ({commit}) {
this.$axios.post('/logout')
commit('SET_USER', null)
}
}
页面加载或执行操作时没有发生任何事情。 git repository 有完整的服务器端和客户端。
UPD 对于那些寻找详细信息的人:
确保您的 nuxtServerInit 函数在 index.js 文件中。你可以这样移动它:
export const actions = {
nuxtServerInit ({commit}, {req}) {
if (req.user) commit('auth/SET_USER', req.user)
}
}
而auth.js 文件已经没有这个功能了。它以这种方式工作。
【问题讨论】: