# 使用
# 动态路由
# 添加路由
router.addRoute({ path: '/about', component: About })
# 删除路由
router.addRoute({ path: '/about', name: 'about', component: About })
# 删除路由
router.removeRoute('about')
# 替换路由
router.replace(router.currentRoute.value.fullPath)
# 查看现有路由
# 检查路由是否存在
router.hasRoute()
# 获取包含所有路线记录的数组
router.getRoutes()
# 添加嵌套路由
router.addRoute({ name: 'admin', path: '/admin', component: Admin })
router.addRoute('admin', { path: 'settings', component: AdminSettings })
# 等效于
router.addRoute({
name: 'admin',
path: '/admin',
component: Admin,
children: [{ path: 'settings', component: AdminSettings }],
})
# 添加新路由后hasNecessaryRoute()返回false以避免无限重定向
router.beforeEach(to => {
if (!hasNecessaryRoute(to)) {
router.addRoute(generateRoute(to))
return to.fullPath
}
})
← 安装