tft每日頭條

 > 職場

 > vue路由切換時獲取不到參數

vue路由切換時獲取不到參數

職場 更新时间:2024-08-26 00:12:35
Vue路由傳參的兩種方式query和params

vue路由切換時獲取不到參數(Vue路由傳參的兩種方式query和params)1

點擊打開視頻講解更加詳細

【面試題】Vue路由傳參的兩種方式query和params_哔哩哔哩_bilibili

一、router-link

1. 不帶參數 <router-link :to="{name:'home'}"> <router-link :to="{path:'/home'}"> //name,path都行, 建議用name // 注意:router-link中鍊接如果是'/'開始就是從根路由開始,如果開始不帶'/',則從當前路由開始。 2.帶參數 <router-link :to="{name:'home', params: {id:1}}"> // params傳參數 (類似post) // 路由配置 path: "/home/:id" 或者 path: "/home:id" // 不配置path ,第一次可請求,刷新頁面id會消失 // 配置path,刷新頁面id會保留 // html 取參 $route.params.id // script 取參 this.$route.params.id <router-link :to="{name:'home', query: {id:1}}"> // query傳參數 (類似get,url後面會顯示參數) // 路由可不配置 // html 取參 $route.query.id // script 取參 this.$route.query.id

二、 this.$router.push()

1. 不帶參數 this.$router.push('/home') this.$router.push({name:'home'}) this.$router.push({path:'/home'}) 2. query傳參 this.$router.push({name:'home',query: {id:'1'}}) this.$router.push({path:'/home',query: {id:'1'}}) // html 取參 $route.query.id // script 取參 this.$route.query.id 3. params傳參 this.$router.push({name:'home',params: {id:'1'}}) // 隻能用 name // 路由配置 path: "/home/:id" 或者 path: "/home:id" , // 不配置path ,第一次可請求,刷新頁面id會消失 // 配置path,刷新頁面id會保留 // html 取參 $route.params.id // script 取參 this.$route.params.id

三、 this.$router.replace() (用法同上,push)四、query和params區别與總結:

- query相當于GET請求,頁面跳轉的時候,可以在地址欄看到請求參數

- params相當于POST請求,參數不會在地址欄中顯示

- query類似 get, 跳轉之後頁面 url後面會拼接參數,類似?id=1, 非重要性的可以這樣傳, 密碼之類還是用params刷新頁面id還在

- params類似 post, 跳轉之後頁面 url後面不會拼接參數 , 但是刷新頁面id 會消失

若對您有幫助,請點擊跳轉到B站一鍵三連哦!感謝支持!!!

【面試題】Vue路由傳參的兩種方式query和params_哔哩哔哩_bilibili

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关職場资讯推荐

热门職場资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved