我将我的申请分为server
和client
。我正在运行服务器并在快递中使用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)
}
}
页面加载或执行操作时没有任何操作。 qazxsw poi有完整的服务器端和客户端。
UPD对于那些寻找细节的人:确保你的git repository函数在nuxtServerInit
文件中。你可以这样移动它:
index.js
并且export const actions = {
nuxtServerInit ({commit}, {req}) {
if (req.user) commit('auth/SET_USER', req.user)
}
}
文件不再具有此功能。它以这种方式工作。
auth.js
操作只能在商店index.js文件中使用(或者在创建商店的另一个单词中)。
所以试着把你的nuxtServerInit
移到那里。