动态路由
动态路由的一般使用场景:后端返回一个路由表,前端通过接口拿到后处理。
主要使用的方法是router.addRoute(),只注册新的路由,也就是说,如果新增加的路由与当前位置相匹配,就需要你用 router.push() 或 router.replace() 来手动导航,才能显示该新路由。
router.addRoute({ path: '/about', component: About })
router.replace(router.currentRoute.value.fullPath)
如果你决定在导航守卫内部添加或删除路由,你不应该调用 router.replace(),而是通过返回新的位置来触发重定向:
router.beforeEach(to => {
if (!hasNecessaryRoute(to))
{
router.addRoute(generateRoute(to))
// 触发重定向
return to.fullPath
}
})