一、$ router和$ route的区别
$router : 是路由操作对象,只写对象
$route : 路由信息对象,只读对象
$ router操作路由跳转
this.$router.push({ name:‘hello’, params:{ name:‘word’, age:‘11’ } })
$route读取 路由参数接收
var name = this.$route.params.name;
二、路由跳转方式name 、 path 和传参方式params 、query的区别
path 和 Name路由跳转方式,都可以用query传参
params传参,push里面只能是 name:'xxx',不能是path:'/xxx',因为params只能用name来引入路由,如果这里写成了path,接收参数页面会是undefined!!!
通过params传参
this.$router.push({
name: "newPage", //params对应路由跳转为name方式
params: {
id: this.id
}
});
另一个页面接收:
console.log(this.$route.params.id)
通过query传参
this.$router.push({
path: "/newPage", //query对应路由跳转为path方式
query: {
id: res
}
});
另一个页面接收:
console.log(this.$route.query.id)
另外,二者还有点区别,直白的来说query相当于get请求,页面跳转的时候,可以在地址栏看到请求参数
而params相当于post请求,参数不会再地址栏中显示
注意:params传参如果路由上面不写参数,也是可以传过去的,但不会在url上面显示出你的参数,并且当你跳到别的页面或者刷新页面的时候参数会丢失
版权声明:本文为hong521520原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。