代码纠正
由于搜索框(带params参数)与展示页面(带query参数)分布在两个组件,所以需要进行路由传参。但在实现过程中发现:通过搜索框从主页跳转至展示组件时能带上params参数,但在展示页面内输入搜索则无法再传递params参数。
原代码如下:(点击搜索调用goSearch函数)
goSearch() {
console.log(this.keyword)
let location = {
name: "Search",
path:"/Search",
params: {
keyword: this.keyword || undefined,
},
query:this.$route.query
};
this.$router.push(location);
解决办法:将this.$router.push改为this.$router.replace
goSearch() {
console.log(this.keyword)
let location = {
name: "Search",
path:"/Search",
params: {
keyword: this.keyword || undefined,
},
query:this.$route.query
};
this.$router.replace(location);