vue-router中query和params傳參的區別

關注我的個人博客:https://pinbolei.cn,獲取更多內容

$router 和 $route的區別

首先說一下 $router和 $route

this.$router 可操作路由對象,只寫對象
this.$route 路由信息對象,只讀對象

1.query

1.路由跳轉

this.$router.push({
  path:'/user',
  params:{
    name:'abc',
    age:'11'
  }
})

2.接受參數

let name = this.$route.query.name;
let age= this.$route.query.age;

2.params

1.路由跳轉

this.$router.push({
  name:'user',
  params:{
    name:'abc',
    age:'11'
  }
})

2.接受參數

let name = this.$route.params.name;
let age= this.$route.params.age;

總結

1.query相當於get傳參,即在瀏覽器地址欄中顯示參數。
2.params相當於post,即在瀏覽器地址欄中不顯示參數,但是如果使用params傳值的話,頁面一刷新params 的值就會消失。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章