发现问题
最近在打开项目的时候,发现我的默认路由没加载上linkactiveclass,
网上一搜,发现很多同学也有这个问题,查了一些资料发现这是个重定向的问题,官网文档是这么写的
https://router.vuejs.org/zh-cn/essentials/redirect-and-alias.html
重定向
重定向也是通过 routes 配置来完成,下面例子是从 /a 重定向到 /b:
const router = new vuerouter({routes: [{ path: '/a', redirect: '/b' }]})
重定向的目标也可以是一个命名的路由:
const router = new vuerouter({routes: [{ path: '/a', redirect: { name: 'foo' }}]})
甚至是一个方法,动态返回重定向目标:
const router = new vuerouter({routes: [{ path: '/a', redirect: to => {// 方法接收 目标路由 作为参数// return 重定向的 字符串路径/路径对象}}]})
我的代码本来是这样的:
const router=new vuerouter({linkactiveclass:'list-active',routes:[{ path:'/', component:user},{ path:'/user', component:user},{ path:'/warship', component:warship}]})
这样虽然加载了子路由,但是它的默认类没跟着过来,然后加了一句redirect:'/user',修改成了这样
修改后:
const router=new vuerouter({linkactiveclass:'list-active',routes:[{ path:'/', redirect:'/user', component:user},{ path:'/user', component:user},{ path:'/warship', component:warship}]})
就完美解决了默认路由class没加载的问题。
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
使用vue如何实现nav导航栏
在vue组件中如何使用iframe元素
在vue中如何加载组件webpack require.ensure
在angular4中如何实现表单响应
以上就是在vue.js中有关默认路由不加载问题的详细内容。
