通过router-link我们可以向路由到的组件传递参数,这在我们实际使用中时非常重要的。
路由:
{ path:"/DetailPage", component: DetailPage, name: "详情页" },
router-link:
<router-link :to="{path: ‘/DetailPage‘, query: {index: index}}"> <div class="item-img"> <p>路由<p> </div> </router-link>
DetailPage页面:
<p>{{$route.query.index}}</p>
即: 在DetailPage页面我们可以通过$route.query.index 来获取到router-link中的值,因为router-link使用的时v-bind:,所以我们传递的index: index中的值index是可以变化的,即动态传递。
这样的结果是在路由切换时,在url中出现查询字符串即 localhost:8080/#/DetailPage?index=0
这是在template中获取,如果需要在js中获取,使用 this.$route.query.index 即可获取。
延伸阅读: http://www.jianshu.com/p/cb918fe14dc6
参考文章: http://www.mamicode.com/info-detail-1575683.html
时间: 2024-12-18 08:05:40