【问题标题】:How can I description localstorage in route of Nuxt.js如何在 Nuxt.js 的路由中描述本地存储
【发布时间】:2020-07-20 01:24:46
【问题描述】:

我需要在每一页检查我在路由器中的令牌。我需要使用beforeEach。我在vue中做。但我不在 nuxt.js 中使用。我的问题如何在 nuxt.js 的路由器中使用我的本地存储。我在route.js 文件中的代码示例是这样的。

export default ({ app, store, redirect }) => {

app.router.beforeEach((to, from, next) => {
    store.state.tokenim = localStorage.getItem("token");

    if (store.state.tokenim == '') 
    next();
})}

我是这样写的。我在这样的控制台中犯了一个错误。

[vue-router] uncaught error during route navigation:                                                                                                                               
 ERROR  localStorage is not defined

请帮忙:)

【问题讨论】:

    标签: javascript nuxt.js router


    【解决方案1】:

    当你将 route.js 添加为插件时,你应该设置 ssr:false

    plugins: [
        { src: '~/plugins/hello.js', ssr: false }
    ]
    

    【讨论】:

      猜你喜欢
      • 2018-05-06
      • 2019-08-26
      • 1970-01-01
      • 2019-04-24
      • 2022-07-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      相关资源
      最近更新 更多