vue中params和query的区别及路由传参的三种基本方式

待写
待写

query

query通过 router-link或者this.$router.push跳转页面传参数

第一种方式 router-link

name和path都可以用,to的取值可以是对象的方式也可以是字符串的方式

<router-link :to="{ name: 'W', query: { id:'1234',age:'12' }}"/>
<router-link :to="{ path: '/W/s', query: { id:'1234',age:'12' }}"/>
<router-link to="'/W/s/?id=1234&age=12'">用户</router-link>
第二种方式 this.$router.push
this.$router.push({path:'/W/s', query: {id:'1234', age: 12}})
this.$router.push({path:'/W/s?id=1234&age=12'})
对应的router编写

name和path都可以来匹配对应的跳转组件

{
	path: '/W/s', 
	name: 'W', 
	component: W
}
形成的页面路径
http://localhost:8080/#/W/s?id=1234&age=12
query接受传递的参数取值
this.$route.query.id
this.$route.query.age

params

query通过 router-link或者this.$router.push跳转页面传参数

第一种方式 router-link

注意:使用router-link不能用path只能用name或者to字符串拼接,不然会直接无视掉params中的内容

<router-link :to="{ name: 'W', params: { id:'1234',age:'12' }}"/>
<router-link to="/W/s/1234/12"/>
第二种方式 this.$router.push
这种方式说是页面刷新数据,数据会丢失(待验证)
this.$router.push({name:'W',params: {id: 1234,age:'12'}})
this.$router.push({path:'/W/s/1234/12'})
对应的router编写

name和path都可以来匹配对应的跳转组件

{
	path: '/W/s/:id/:age', 
	name: 'W', 
	component: W
}
形成的页面路径
http://localhost:8080/#/W/s/1234/12
query接受传递的参数取值
this.$route.params.id
this.$route.params.age

params和query的区别

虽然query使用path或者name引入都可以,但是params只能使用name来引入,接受参数类似,分别是this.$route.query.id和this.$route.params.id
query拼接的路径,类似是get请求
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值