首页 > 编程知识 正文

路由传参的三种方式,路由传参的两种方式

时间:2023-05-03 09:11:55 阅读:241788 作者:3929

路由带参数:

传参方式可划分为 params 传参和 query 传参,而 params 传参又可分为在 url 中显示参数和不显示参数两种方式

1.params 传参(显示参数)又可分为 声明式 和 编程式 两种方式

声明式router-link:该方式是通过router-link组件的to属性实现,子路由需要提前配置好参数。 <router-link :to="/child/1"> 跳转到子路由 </router-link> { path: '/child/:id', component: Child} 编程式 this.$router.push:同样需要子路由提前配置好参数。 { path: '/child/:id', component: Child} this.$router.push({ path:'/child/${id}',})

接收: this.$route.params.id

2. params传参(不显示参数)也可分为声明式和编程式两种方式,与显示参数不同的是,这里是通过路由的别名 name 进行传值的

<router-link :to="{name:'Child',params:{id:1}}">跳转到子路由</router-link> { path: '/child, name: 'Child', component: Child} this.$router.push({ name:'Child', params:{ id:1 }})

接收: this.$route.params.id

3.query 传参(显示参数)也可分为声明式和编程式 两种方式

声明式router-link:该方式是通过 router-link 组件的 to 属性实现,不过使用该方式传值的时候,需要子路由提前配置好路由别名 { path: '/child, name: 'Child', component: Child} <router-link :to="{name:'Child',query:{id:1}}">跳转到子路由</router-link> 编程式 this.$router.push:使用该方式传值的时候,同样需要子路由提前配置好路由别名(name 属性) { path: '/child, name: 'Child', component: Child} this.$router.push({ name:'Child', query:{ id:1 }})

接收: this.$route.query.id

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。