import { createMemoryHistory, createRouter } from "vue-router";
import { AuthUser } from "../../auth/user";

const routes = [
  {
    path: "/",
    component : () => import("../components/Home/Home.vue"),
    beforeEnter : (to, from, next)=>{
      if(!AuthUser()){
        next({path : "/signup"})
      }else{
        next()
        return ""
      }
    }
  },
  {
    path: "/signup",
    component : () => import("@/components/Signup/Signup.vue"),
  }
];

const router = createRouter({
  history: createMemoryHistory(),
  routes,
});

export default router
 

我不知道为什么当我打开"/"时,组件注册显示,但网址"http://localhost:3000/"根本没有改变.

它完美地显示了组件,问题在于网址.

Authuser目前始终返回true.

推荐答案

发生这种情况是因为您使用createMemoryHistory()作为历史记录模式,该模式不与浏览器兼容,但用于测试和服务器端渲染,因此您需要使用createWebHistory()来利用浏览器的历史记录.

import { createWebHistory, createRouter } from 'vue-router';
import { AuthUser } from "../../auth/user";

const routes = [
  {
    path: "/",
    component : () => import("../components/Home/Home.vue"),
    beforeEnter : (to, from, next) => {
      if (!AuthUser()) {
        next({path : "/signup"});
      } else {
        next();
      }
    }
  },
  {
    path: "/signup",
    component : () => import("@/components/Signup/Signup.vue"),
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

export default router;

Javascript相关问答推荐

单击按钮后未清除 Select

reaction如何在不使用符号的情况下允许多行返回?

*ngFor和@代表输入decorator 和选角闭合

使用useup时,React-Redux无法找到Redux上下文值

类型脚本中只有字符串或数字键而不是符号键的对象

InDesign—创建一个独立的窗口,在文档中进行更正时保持打开状态

判断表格单元格中是否存在文本框

在网页上添加谷歌亵渎词

在react js中使用react—router—dom中的Link组件,分配的右侧不能被 destruct ''

如果Arrow函数返回函数,而不是为useEffect返回NULL,则会出现错误

编辑文本无响应.onClick(扩展脚本)

在forEach循环中获取目标而不是父对象的属性

JavaScript是否有多个`unfined`?

无法读取未定义的属性(正在读取合并)-react RTK

我想使用GAS和HTML将从Electron 表格中获得的信息插入到文本字段的初始值中

OnClick更改Json数组JSX中的图像源

用于部分字符串的JavaScript数组搜索

React:防止useContext重新渲染整个应用程序或在组件之间共享数据而不重新渲染所有组件

MongoDB中的嵌套搜索

Reaction即使在重新呈现后也会在方法内部保留局部值