vuex将状态更新值存储在中间件check-auth.js中,但无法在vue视图中获取更新后的值

时间:2019-01-17 08:50:54

标签: vue.js vuex nuxt.js

我正在为我的项目使用nuxtjs,我使用axios在minddleware / check-auth.js中获取身份验证信息,并将标志提交到store.state.isSEPCNavFlag;。 但是我无法在vue视图中获取更新的isSEPCNavFlag值,该值始终为null。 有人可以帮助我吗?请...

store / index.js

import axios from 'axios'
import echarts from 'echarts'
import Vue from 'vue'
import Vuex from 'vuex'
import { saveToken, delToken } from '@/utils/auth'
Vue.use(Vuex)

export const strict = true

export const state = () => ({
  authUser: null,
  user: '',
  locale: null,
  locales: ['zh', 'en'],
  isMenuHidden: true,
  isSEPCNavFlag:null,
})

export const mutations = {
  SET_SEPCFLAG: function (state, isSEPCNavFlag) {
      state.isSEPCNavFlag = isSEPCNavFlag
    },

  SET_TOKEN: function (state, token) {
    state.authUser = token
  },
  SET_USER: function (state, user) {
    state.user = user
  },
  SET_MENUS: function (state, menus) {
    state.menus = menus
  },
  SET_LANG (state, locale) {
    if (state.locales.indexOf(locale) !== -1) {
      state.locale = locale
    }
  },
  TOGGLE_MENU_HIDDEN: function (state) {
    state.isMenuHidden = !state.isMenuHidden
  }
}

minddleware / check-auth.js

async function toNavTO(token,store) {
  var returnData = await funcAsync(token)
  var ACNameIndex = returnData.data.indexOf("<AccountName>");
  var navFlag = true;
  if(ACNameIndex == -1){
        navFlag=false
    }
  store.commit('SET_SEPCFLAG', navFlag)
}

index.vue

mounted(){
  this.login();
},
methods: {
    login(){
      let navFlag = this.$store.state.isSEPCNavFlag;
      console.log(navFlag);

this。$ store.state.isSEPCNavFlag值始终为空。

mutations SET_SEPCFLAG console.log

0 个答案:

没有答案