项目中很多情况下都需要进行路由之间的传值,想过很多种方式 sessionstorage/localstorage/cookie 进行离线缓存存储也可以,用vuex也可以,不过有些大材小用吧,不管怎么说因场景而异 下面我来说下vue自带的路由传参的三种基本方式 先有如下场景 点击当前页的某个按钮跳转到另外一个页面去,并将某个值带过去
1 |
<div class="examine" @click="insurance(2)">查看详情</div> |
第一种方法 页面刷新数据不会丢失
1 2 3 4 5 6 7 |
methods:{ insurance(id) { //直接调用$router.push 实现携带参数的跳转 this.$router.push({ path: `/particulars/${id}`, }) } |
需要对应路由配置如下:
1 2 3 4 5 |
{ path: '/particulars/:id', name: 'particulars', component: particulars } |
可以看出需要在path中添加/:id来对应 $router.push 中path携带的参数。在子组件中可以使用来获取传递的参数值 另外页面获取参数如下
1 |
this.$route.params.id |
第二种方法 页面刷新数据会丢失 通过路由属性中的name来确定匹配的路由,通过params来传递参数。
1 2 3 4 5 6 7 8 9 |
methods:{ insurance(id) { this.$router.push({ name: 'particulars', params: { id: id } }) } |
对应路由配置: 注意这里不能使用:/id来传递参数了,因为组件中,已经使用params来携带参数了。
1 2 3 4 5 |
{ path: '/particulars', name: 'particulars', component: particulars } |
子组件中: 这样来获取参数
1 |
this.$route.params.id |
第三种方法 使用path来匹配路由,然后通过query来传递参数 这种情况下 query传递的参数会显示在url后面?id=?
1 2 3 4 5 6 7 8 9 |
methods:{ insurance(id) { this.$router.push({ path: '/particulars', query: { id: id } }) } |
对应路由配置:
1 2 3 4 5 |
{ path: '/particulars', name: 'particulars', component: particulars } |
对应子组件: 这样来获取参数
1 |
this.$route.query.id |
特别注意哦, 组件中 获取参数的时候是router 这很重要~~~ 作者:w夏了夏天 链接:https://www.jianshu.com/p/d276dcde6656 来源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
View Detailsbreak—-用return false; continue --用return true; 代码如下: Object.keys(flatMenuData).forEach((key) => { if (item.routeName === this.$route.name && !rs.activeItem) { //如果开启了首页不展示左侧菜单,则屏蔽左侧菜单 if(this.inner_sideMenu.opt.notActiveIndex){ if(this.$route.name ==’index'){ return true } } } } ———————————————— 版权声明:本文为CSDN博主「CarryBest」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/carrybest/article/details/89000582
View Details