如何在Nuxt中使用nuxtServerInit作为中间件

问题描述 投票:1回答:2

我将我的申请分为serverclient。我正在运行服务器并在快递中使用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) } } 文件不再具有此功能。它以这种方式工作。

node.js nuxt.js
2个回答
1
投票

auth.js操作只能在商店index.js文件中使用(或者在创建商店的另一个单词中)。

所以试着把你的nuxtServerInit移到那里。

© www.soinside.com 2019 - 2024. All rights reserved.