繁体   English   中英

nuxt.js !process.server 永远不会出现在 if 语句中

[英]nuxt.js !process.server never gets in the if statement

大家好,我有以下问题:

我有一个名为“仪表板”的 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”或您寻找其他解决方案

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM