一、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
本文主要介绍了Vue中params和query两种路由参数。params参数是路径一部分,配置路由时需占位,可通过字符串、模版字符串、对象形式传递,用$route.params.keyword获取;query参数不属于路径部分,以path?k=val形式存在,传递和获取方式与params类似。
525

被折叠的 条评论
为什么被折叠?



