vuejs / vue-router

🚦 The official router for Vue 2
http://v3.router.vuejs.org/
MIT License
18.99k stars 5.06k forks source link

addRoute() did not work #3896

Closed 2849232wy closed 2 months ago

2849232wy commented 3 months ago

Version

3.6.5

Reproduction link

github.com

Steps to reproduce

  1. set up routes
    const routes = [
    {
    path: "/",
    name: "layout",
    component: () => import("@/views/Layout/index.vue"),
    redirect: "/home",
    children: [
      {
        path: "home",
        icon: "icon-home1",
        title: "首页",
        name: "home",
        component: () => import("@/views/Home/index.vue"),
      },
    ],
    },
    {
    path: "/login",
    title: "登录",
    component: () => import("@/views/Login/index.vue"),
    },
    ];

export default routes;

  1. send request and get dynamicMenu
    submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            let res = await login(this.form);
            if (res.code === 20000) {
              this.$message({
                type: "success",
                message: "登录成功",
              });
              initDynamicRoutes(res.data.menu);
              this.$router.replace("/home");
            }
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
  2. set up dynamicMenu
    
    import routes from "./routes";
    import vueRouter from "vue-router";
    import store from "@/store";
    const Goods = () => import("@/views/Goods/index.vue");
    const User = () => import("@/views/User/index.vue");
    const Pages1 = () => import("@/views/PagesOne/index.vue");
    const Pages2 = () => import("@/views/PagesTwo/index.vue");

let MappingRoutes = { Goods, User, Pages1, Pages2, };

const router = new vueRouter({ routes, mode: "hash", }); router.beforeEach((to, from, next) => { let localStorageToken = localStorage.getItem("token"); if (!to.fullPath.includes("login")) { if (!localStorageToken) { next("/login"); } else { // store的token丢失后重新赋值 store.dispatch("tokenStore/setToken", localStorageToken); next(); } } else { next(); } }); export const initDynamicRoutes = (currentRoutes) => { handleDynamicRoutes(currentRoutes); currentRoutes.forEach((route) => { router.addRoute("layout", route); }); console.log(router.getRoutes); };

const handleDynamicRoutes = (currentRoutes) => { currentRoutes.forEach((element) => { element.component = MappingRoutes[element.component]; if (element.children && element.children.length > 0) { handleDynamicRoutes(element.children); } }); };

export default router;



### What is expected?
Dynamic route addition is invalid

### What is actually happening?
Dynamic routes were successfully added

---
vueRouter's dynamic funcation

<!-- generated by vue-issues. DO NOT REMOVE -->
posva commented 2 months ago

Hi, thanks for your interest but Github issues are for bug reports and feature requests only. You can ask questions on the forum, the Discord server or StackOverflow.

posva commented 2 months ago

The code you provided is a whole project. Since addRoute() hasn't changed in a few years, chances are that addRoute() is working as expected or there is already an opened issue or pr about this.