一、params参数
params参数:属于路径当中的一部分,在配置路由时需要占位。
比如:
path: '/search/:keyword'
传递参数
1、字符串形式
this.$router.push("/search/" + this.keyword);
2、模版字符串形式
this.$router.push(`/search/${this.keyword}`);
3、对象形式
使用对象形式传递参数时,需要用name或着path指定要跳转的路径,注意,path和params不能同时使用。
this.$router.push({name: "search",params: { keyword: this.keyword }});
获取参数
$route.params.keyword
二、query参数
query参数:不属于路径当中的一部分,是 path?k=val 的一个形式,不需要占位
传递参数
1、字符串形式
this.$router.push("/search?k=" + this.keyword);
2、模版字符串形式
this.$router.push("/search?k=" + this.keyword);
3、对象形式
使用对象形式传递参数时,需要用name或着path指定要跳转的路径,注意,path和params不能同时使用。
this.$router.push({name: "search",query: { k: this.keyword }});
获取参数
$route.query.k