【问题标题】:nuxt.js !process.server never gets in the if statementnuxt.js !process.server 永远不会进入 if 语句
【发布时间】:2020-02-25 20:05:04
【问题描述】:

大家好,我有以下问题:

我有一个名为“仪表板”的 nuxt.js 页面,我为它制作了一个中间件,如下所示:

export default {
   components: {
      overview
   },
   middleware: "authentication"
};

在我的 authentication.js 文件中:

  import axios from "axios";
  export default async function({ store, redirect }) {
  if (!store.state.authentication.authenticated) {
    if (!process.server) {
      console.log("i am in statement");
      if (!window.localStorage.getItem("realtime_jwt_token")) {
        return redirect("/");
      } else {
        const checkJwt = await axios.post("/api/checkToken", {
          token: window.localStorage.getItem("realtime_jwt_token")
        });
        if (checkJwt.data) {
          return null;
        }
        window.localStorage.removeItem("realtime_jwt_token");
        return redirect("/");
      }
    }
  }
}

console.log("i am in statement") 永远不会显示在我的浏览器中,也不会显示在服务器控制台上,我总是能够访问仪表板站点。 为什么!process.server 不在这里工作?

编辑:

我找到了原因。

如果您将模式设置为“通用”,则中间件会在服务器端运行一次。 要访问客户端,您应该将模式设置为“spa”,或者您寻找其他解决方案

【问题讨论】:

    标签: javascript vue.js nuxt.js


    【解决方案1】:

    您可能不是 process.server 有问题,而是之前的语句“store.state.authentication.authenticated”有问题。你用上面的另一个 console.log 检查了吗?

    【讨论】:

    • 这应该是对他的问题的评论,而不是答案
    • 听说过新的用户限制吗?
    猜你喜欢
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 2020-09-01
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多