Vue路由使用总结
1、如何使用 router-link
对象方式传递参数?
在 vue-router
中,有两大对象被挂载到了实例 this
;
$route(只读、具备信息的对象)
;
$router(具备功能的函数)
查询字符串:
-
去哪里 ?
<router-link :to="{name:'detail',query:{id:1}}"> xxx </router-link>
-
导航(查询字符串path不用改)
{name:'detail',path:'/detail',组件}
-
去了干嘛?获取路由参数(要注意是 query ,还是 params 和 对应的
id
名? 是后者需要注意设置相关文件的id规则)+ this.$route.query.id
path方式:
- 去哪里 ?
<router-link :to="{name:'detail',params:{name:1}}"> xxx </router-link>
- 导航(查询字符串path不用改)
{name:'detail',path:'/detail/:name',组件}
- 去了干嘛?获取路由参数(要注意是 query ,还是 params 和 对应的
id
名? 是后者需要注意设置相关文件的id规则)+ this.$route.params.id
2、多级菜单配置
每个路由的位置必须指定 <router-view/>
页面显示的容器,多级路由的情况需要在父路由跳转的页面中添加自路由的 <router-view/>
页面