路由传参:
传参方式可分为params传参和query传参,其中params又可分为url中显示参数和不显示参数
1、params传参(显示参数)
- 声明式:
router-link
该方式通过router-link的to属性实现,子路由需要提前配置好参数
{
path: '/child/:id',
component: Child
}
<router-link :to="/child/1"> 跳转到子路由 </router-link>
- 编程式:
this.$router.push
该方式同样需要子路由提前配置好参数
{
path: '/child/:id',
component: Child
}
this.$router.push({
path:'/child/${id}',
})
接收: this.$route.params.id
2、params传参(不显示参数)
也可分为声明式和编程式两种方式,与显示参数不同的是,这里是通过路由的别名 name 进行传值的
{
path: '/child,
name: 'Child',
component: Child
}
<router-link :to="{name:'Child',params:{id:1}}">跳转到子路由</router-link>
this.$router.push({
name:'Child',
params:{
id:1
}
})
接收: this.$route.params.id
3、query传参(显示参数)
- 声明式:
router-link
{
path: '/child,
name: 'Child',
component: Child
}
<router-link :to="{name:'Child',query:{id:1}}">跳转到子路由</router-link>
- 编程式:
this.$router.push
{
path: '/child,
name: 'Child',
component: Child
}
this.$router.push({
name:'Child',
query:{
id:1
}
})
接收: this.$route.query.id
本文详细介绍了Vue.js中路由参数传递的三种方式:params传参(包括显示参数和不显示参数)以及query传参。params传参适用于隐藏参数或基于路径的结构,而query传参则会显示在URL中。无论是声明式还是编程式,传参和接收参数的方法都进行了清晰的演示。通过示例代码,读者可以更好地理解和应用这些传参方法。


&spm=1001.2101.3001.5002&articleId=124340403&d=1&t=3&u=6557281373884f7c9d667e52eb1167f5)
4696

被折叠的 条评论
为什么被折叠?



