Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript I';我试图访问中间件路由器保护中的存储数据(getter),但返回false_Javascript_Vue.js_Axios - Fatal编程技术网

Javascript I';我试图访问中间件路由器保护中的存储数据(getter),但返回false

Javascript I';我试图访问中间件路由器保护中的存储数据(getter),但返回false,javascript,vue.js,axios,Javascript,Vue.js,Axios,我正在为尚未在系统中创建子帐户的新用户创建一个新页面“/gettingstarted”。 因此,根据我们的存储数据,如果用户没有帐户,则每次用户导航到另一个页面时,我都必须将其重定向到“/gettingstarted”页面,但问题是每次我刷新页面时,“都有帐户”getter在页面重新加载后呈现,这是有意义的,因为路由器防护就是这样的。但是我需要一种方法来解决这个问题,让路由器守卫等待getter 中间件.js Router.beforeEach(function (to, from, next)

我正在为尚未在系统中创建子帐户的新用户创建一个新页面“
/gettingstarted
”。 因此,根据我们的存储数据,如果用户没有帐户,则每次用户导航到另一个页面时,我都必须将其重定向到“
/gettingstarted
”页面,但问题是每次我刷新页面时,“
都有帐户
”getter在页面重新加载后呈现,这是有意义的,因为路由器防护就是这样的。但是我需要一种方法来解决这个问题,让路由器守卫等待getter

中间件.js

Router.beforeEach(function (to, from, next) {
  if (!store.getters["accounts/hasAccount"]) {
    next({
      path: '/gettingstarted'
    })
  } else {
    next()
  }
})
 hasAccount(state) {
      if (state.accounts.data) {
        if (state.accounts.data.length > 0) {
          return true
        } else return false
      } else return false
    },
这是获取真与假的getter

store.js

Router.beforeEach(function (to, from, next) {
  if (!store.getters["accounts/hasAccount"]) {
    next({
      path: '/gettingstarted'
    })
  } else {
    next()
  }
})
 hasAccount(state) {
      if (state.accounts.data) {
        if (state.accounts.data.length > 0) {
          return true
        } else return false
      } else return false
    },

重新加载页面将重新初始化vuex。因此,
state.accounts.data
应该与cookies或localStorage一起保存

我实际上将我的
accounts.data
存储在本地存储
localStorage.setItem('hasAccount',accounts.data)
中,这对我很有效。然后我用
if(!localStorage.getItem('hasAccount)
替换了代码中的
if(!store.getters[“accounts/hasAccount”])
,结果成功了