了解Nuxt中间件

时间:2019-06-05 07:57:57

标签: vue.js vuejs2 nuxt.js

我试图弄清楚nuxt中间件的工作方式,我想添加身份验证检查,但似乎无法访问初始状态,我需要初始状态的属性才能在会话检查逻辑中使用。

nuxt.config.js

module.exports = {
  ...
  router: {
    middleware: ["auth"]
  },
  ...
}
const init_state = {
  token: null,
  app: {
    name: process.env.NUXT_ENV_APP_NAME,
    settings: {}
  },
  permissions: {},
  mainMenu: process.env.NUXT_ENV_MENU,
  setTimerFlag: false,
  session: {
    entity: {},
    slo_time: 60,
    styling: process.env.NUXT_ENV_BRAND_THEME
  }
};

auth.js-理想情况下,我想使用context.store ||访问init_state。 context。$ store

export default function(context) {

  console.log("context", context.store) // [a] not showing intial_state object

  console.log("context", context.$store) // [b] undefined

  // [c] so i created what i think is an ok workaround, 
  // use getters to get the whole intial_state object 
  console.log("context", context.store.getters.state) // I get init_state object


}

store.js

const createStore = () => {
  return new Vuex.Store({
    state: init_state, // [a][b] i thought this would be available in middleware
    mutations: {...},
    actions: {...},
    getters: {
      state(state) { // [c] workaround
        return state;
      }
    }
  })
}

所以我的问题-在中间件中还有另一种(更好的)访问初始状态对象的方法吗?

0 个答案:

没有答案
相关问题