Closed 2849232wy closed 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.
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.
Version
3.6.5
Reproduction link
github.com
Steps to reproduce
export default routes;
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;