源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

vue.js默认路由不加载linkActiveClass问题的解决方法

  • 时间:2022-07-28 10:21 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:vue.js默认路由不加载linkActiveClass问题的解决方法
[b]发现问题[/b] 最近在打开项目的时候,发现我的默认路由没加载上linkActiveClass, [img]http://files.jb51.net/file_images/article/201712/20171211143723632.png?20171111143734[/img] 网上一搜,发现很多同学也有这个问题,查了一些资料发现这是个重定向的问题,官网文档是这么写的 [url=https://router.vuejs.org/zh-cn/essentials/redirect-and-alias.html]https://router.vuejs.org/zh-cn/essentials/redirect-and-alias.html[/url] [b]重定向[/b] 重定向也是通过 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 重定向的 字符串路径/路径对象
}}
]
})
[b]我的代码本来是这样的:[/b]
const router=new VueRouter({
linkActiveClass:'list-active',
routes:[
{
 path:'/',
 component:user
},
{
 path:'/user',
 component:user
},
{
 path:'/warship', 
 component:warship
}
]
})
这样虽然加载了子路由,但是它的默认类没跟着过来,然后加了一句redirect:'/user',修改成了这样 [b]修改后:[/b]
const router=new VueRouter({
linkActiveClass:'list-active',
routes:[
{
 path:'/',
 redirect:'/user',
 component:user
},
{
 path:'/user',
 component:user
},
{
 path:'/warship', 
 component:warship
}
]
})
就完美解决了默认路由class没加载的问题。 这个重定向简单来说就是自定义路由指针,就跟js里面修改了引用地址一个道理,虽然表面上看着是个根目录,其实引用的是别的路由界面。 [b]总结[/b] 以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对编程素材网的支持。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部