Vue3项目小bug的解决

router.addRouters()方法报错

Uncaught (in promise) TypeError: router__WEBPACK_IMPORTED_MODULE_2_.default.addRouters is not a function
原因:
新版VueRouter废除了addRoutes();//添加的数组
改为addRoute(RouteRecordRaw);//添加的为对象
原代码为:

const routers = store.getters.addRouters;
router.addRoutes(routers);

改为:

const routers = store.getters.addRouters;
routers.forEach((route) => {
  router.addRoute(route);
});

即可解决。

404正则匹配

Uncaught (in promise) Error: Catch all routes ("*") must now be defined using a param with a custom regexp.
原因:
vue3对404配置进行了修改,必须要使用正则匹配

  {
    // 匹配所有路径  vue2使用*   vue3使用/:pathMatch(.*)*或/:pathMatch(.*)或/:catchAll(.*)
    path: "/:catchAll(.*)*",
    name: "404",
    component: ()=> import("../components/NoFind.vue")
  }

你可能感兴趣的:(Vue3项目小bug的解决)