An error is reported when the Vue3 project is running: TypeError: router.addRouters is not a function

When we dynamically add routes, the router.addRouters() method reports an error: Uncaught (in promise) TypeError: router.default.addRouters is not a function.

Cause Analysis:

The new version of VueRouter abolishes addRoutes();//added array

Change to addRoute(RouteRecordRaw);//Added as an object

Solution:

// 旧版本写法
const routers = store.getters.addRouters;
router.addRoutes(routers);
// 新版本写法
const routers = store.getters.addRouters;
routers.forEach((route) => {
  router.addRoute(route);
});

can solve the problem

Guess you like

Origin blog.csdn.net/Star_ZXT/article/details/126912184