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