1、router-link跳转
//配置
{
path: '/detail/:id',
name: 'detail',
component: () => import('../views/Detail.vue')
}
//不携带参数跳转
<router-link to="/detail" tag="button">
跳转到detail
</router-link>
//携带参数跳转
<router-link :to="'/detail/'+item.id" tag="button">
跳转到detail
</router-link>
//使用
this.$route.params
//在详情页使用
mounted(){
var id = this.$route.params.id;
console.log(id)
this.axios.get(`v2/movie/subject/${id}`).then(res=>{
console.log(res.data)
this.info = res.data;
})
}
tips:渲染页面直接用定义的数组获取数据,不需循环
2、命名路由
//配置
{
path: '/detail',
/* 命名路由 */
name: 'detail',
component: () => import('../views/Detail.vue')
}
2-1、router-link,get
<router-link :to="{name:'detail',query:{id:1213}}">detail</router-link>
2-2、router-link动态路由
//配置动态路由
{
path: '/detail/:id',
/* 命名路由 */
name: 'detail',
component: () => import('../views/Detail.vue')
}
//配置router-link
<router-link :to="{name:'detail',params:{id:1213}}">detail</router-link>
2-3、$router.push()
this.$router.push({name:"detail",params:{id:1314}})
2-4、props解耦id
//配置路由
{
path: '/detail/:id',
/* 命名路由 */
name:'detail',
component:() => import('../views/Detail.vue'),
//core code
props:true
}
//Detail.vue
export default {
props:['id'],
mounted(){
console.log(this.id)
}
};