Vue Router 的params和query传参的使用和区别(详尽)


1.vue之this.$route.query和this.$route.params的使用与区别

转载https://blog.csdn.net/mf_717714/article/details/81945218?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.edu_weight&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.edu_weight,原文地址

首先简单来说明一下$router$route的区别

//$router : 是路由操作对象,只写对象 //$route : 路由信息对象,只读对象 //操作 路由跳转 this.$router.push({ name:'hello', params:{ name:'word', age:'11' } }) //读取 路由参数接收 this.name = this.$route.params.name; this.age = this.$route.params.age;
//query传参,使用name跳转 this.$router.push({ name:'second', query: { queryId:'20180822', queryName: 'query' } }) //query传参,使用path跳转 this.$router.push({ path:'second', query: { queryId:'20180822', queryName: 'query' } }) //query传参接收 this.queryName = this.$route.query.queryName; this.queryId = this.$route.query.queryId;

 

//params传参 使用name this.$router.push({ name:'second', params: { id:'20180822', name: 'query' } }) //params接收参数 this.id = this.$route.params.id ; this.name = this.$route.params.name ; //路由  { path: '/second/:id/:name', name: 'second', component: () => import('@/view/second') }

 

需要注意的是:

  1. params是路由的一部分,必须要在路由后面添加参数名。query是拼接在url后面的参数,没有也没关系。
  2. params一旦设置在路由,params就是路由的一部分,如果这个路由有params传参,但是在跳转的时候没有传这个参数,会导致跳转失败或者页面会没有内容。

总结

  1. 传参可以使用params和query两种方式。
  2. 使用params传参只能用name来引入路由,即push里面只能是name:’xxxx’,不能是path:’/xxx’,因为params只能用name来引入路由,如果这里写成了path,接收参数页面会是undefined!!!。
  3. 使用query传参使用path来引入路由。
  4. params是路由的一部分,必须要在路由后面添加参数名。query是拼接在url后面的参数,没有也没关系。
  5. 二者还有点区别,直白的来说query相当于get请求,页面跳转的时候,可以在地址栏看到请求参数,而params相当于post请求,参数不会再地址栏中显示。
 

转载https://blog.csdn.net/lsy__lsy/article/details/79991955 ,原版地址

一、this.$route.query的使用

  1、router/index.js

    {
    path:'/mtindex',
    component: mtindex,
    //添加路由
    children:[
     {
      path:':shopid',
      component:guessdetail
     }
    ]    

    },

2、传参数

this.$router.push({
      path: '/mtindex/detail', query:{shopid: item.id}

        });

3、获取参数

this.$route.query.shopid

4、url的表现形式(url中带有参数)

http://localhost:8080/#/mtindex/detail?shopid=1

二、this.$route.params

1、router/index.js

{
    path:'/mtindex',
    component: mtindex,
    //添加路由
    children:[
     {
      path:"/detail",
      name:'detail',
      component:guessdetail
     }
    ]    

    },

2、传参数( params相对应的是name  query相对应的是path)

this.$router.push({
      name: 'detail', params:{shopid: item.id}

        });

3、获取参数

this.$route.params.shopid

4、url的表现形式(url中没带参数)

http://localhost:8080/#/mtindex

 

 


					


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM