|
动态路由:
1.配置动态路由,在 main.js文件中 【备注:动态路由参数以冒号开头】- const routes = [
- { path: '/home', component: Home },
- { path: '/news', component: News },
- { path: '/info/:id', component: Info },
- { path: '*', component: Home }, //默认首页
- ]
复制代码
2.在 news 组件中放入跳转的参数【就是跳转之前的组件】- <li v-for="(item,key) in list">
- <router-link :to="'/info/'+key">{{item}}</router-link>
- </li>
复制代码
3.在 info组件中利用 this.$route.params 接收传递来的参数,注意生命周期【就是跳转后的组件】- ,mounted(){ //在组件加载的时候就接收
- console.log(this.$route.params)
- }
复制代码 咳咳 下面拓展一下 传两个值怎么写- <router-link :to="'/info/'+key +'/'+item">{{item}}</router-link>
复制代码- { path: '/info/:id/:name', component: Info },
复制代码
|
|