一、字符串传参方式(params传参和query传参)
this.$router.push('/search/' + this.keyword + '?k=' + this.keyword.toUpperCase());
二、模板字符串传参方式
this.$router.push(`/search/${this.keyword}?k=${this.keyword.toUpperCase()}`);
三、通过params参数进行传参(对象传参方式)
- 通过parmas参数进行传参时,需要在路径中进行占位,需要通过name属性进行使用,path是不能配合params参数一起使用的。
{
name:'search'
path:'/search/:keyword'
component:Search,
meta:{
showFooter:true
}
}
- params参数与query参数可以一起传递,具体用法参考如下:
this.$router.push({
name:'search',
params:{keyword:this.keyword},
query:{k:this.keyword.toUppreCase()}
})
{
name:'search'
path:'/search/:keyword?'
component:Search,
meta:{
showFooter:true
}
}
四、如何指定params参数可以传递也可以不传递
path:"/search/:keyword?"
- 在配置路由的时候,在占位的后面添加一个问号表示params参数可传可不传
- 如果params传递的是空串,可以通过undefined解决
this.$router.push({
name:'search',
params:{keyword:'' || undefined}, //如果传递的是空字符串,返回undefined
query:{k:this.keyword.toUppreCase()}
})
五、路由组件通过props传递数据
{
path:"/search/:keyword",
component:Search,
meta:{
showFooter:true
},
props:true
}