vue-router传参有两种方式:params,query

1.params方式:

this.$router.replace({
  name:'xx',
  params:{
    username:this.userName,
    password:this.password
  }
})

获取参数:this.$route.params.username

2.query方式

this.$router.replace({
  path:'/xx',
  query:{
    username:this.userName,
    password:this.password
  }
})

获取参数:this.$route.query.username

简单来说,params方法相当于post请求,数据不会显示在地址栏中;而query请求相当于get请求,数据会显示在地址栏中。另外用params传递数据时replace里面只能是name:'xx',否则即使可以跳转页面,也没有传递的数据;

还有获取数据是$route,在这里说一下$router和$route的区别:$router是vue-router实例,所有和路由相关的东西都包含在$router中,包括$route,想要跳转路由可以通过push,replace等方法。$route只是跟当前路由有关的数据,比如path,query,params之类的

 

Logo

前往低代码交流专区

更多推荐