nuxt.js!process.server永远不会进入if语句

时间:2020-02-25 20:05:04

标签: javascript vue.js nuxt.js

大家好,我遇到以下问题:

我有一个称为“ dashboard”的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”,或者您正在寻找其他解决方案

1 个答案:

答案 0 :(得分:-1)

您可能不是问题process.server,而是先前的语句“ store.state.authentication.authenticated”。您是否已通过上面的另一个console.log进行了检查?