Vue router傳遞參數(shù)并解決刷新頁(yè)面參數(shù)丟失問(wèn)題
Vue Router 傳參方式:
1. this.$router.push({ name: '模塊名稱(chēng)', params: { // 各參數(shù) } })
router.js:
export default new Router({ routes: [ { path: '/paramsPassingByRouter', component: ParamsPassingByRouter, children: [ { path: 'paramsMode', name: 'paramsMode', component: ParamsMode } ] } ] })
ParamsPassingByRouter.vue:
<!-- html --> <button @click="paramsMode(testData)">params傳參</button> <!-- js --> <script> export default { data () { return { testData: { id: '20180101', name: '張三', aka: 'z3', age: '18' } } }, methods: { paramsMode (data) { this.$router.push({ name: 'paramsMode', params: data }) } } } </script>
ParamsMode.vue:
<!-- html --> <div class="params-mode">{{ testData }}</div> <!-- js --> <script> export default { data () { return { testData: {} } }, created () { this.testData = this.$route.params } } </script>
效果:
url:http://localhost:8081/#/paramsPassingByRouter/paramsMode
頁(yè)面顯示:{"id":"20180101","name":"張三","aka":"z3","age":"18"}
但是刷新頁(yè)面后,數(shù)據(jù)會(huì)丟失,顯示:{}。
2. this.$router.push({ name: '模塊名稱(chēng)', query: { // 各參數(shù) } })
router.js:
export default new Router({ routes: [ { path: '/paramsPassingByRouter', component: ParamsPassingByRouter, children: [ { path: 'queryMode', name: 'queryMode', component: QueryMode } ] } ] })
ParamsPassingByRouter.vue:
<!-- html --> <button @click="queryMode(testData)">query傳參</button> <!-- js --> <script> export default { data () { return { testData: { id: '20180101', name: '張三', aka: 'z3', age: '18' } } }, methods: { queryMode (data) { this.$router.push({ name: 'paramsMode', query: data }) } } } </script>
QueryMode.vue:
<!-- html --> <div class="query-mode">{{ testData }}</div> <!-- js --> <script> export default { data () { return { testData: {} } }, created () { this.testData = this.$route.query } } </script>
效果:
url:http://localhost:8081/#/paramsPassingByRouter/queryMode?id=20180101&name=%E5%BC%A0%E4%B8%89&aka=z3&age=18
頁(yè)面顯示:{"id":"20180101","name":"張三","aka":"z3","age":"18"}
刷新頁(yè)面后,數(shù)據(jù)不會(huì)丟失。
解決刷新頁(yè)面數(shù)據(jù)丟失的方案:
使用 this.$router.push({ name: '模塊名稱(chēng)', query: { // 各參數(shù) } }) 方式傳參。
缺點(diǎn):參數(shù)值都拼接在 url 上,url 會(huì)很長(zhǎng),同時(shí)都可被看到。
this.$router.push({ name: '模塊名稱(chēng)', params: { // 各參數(shù) } }) 路由文件設(shè)置的時(shí)候把參數(shù)拼到 url 里。
url:http://localhost:8081/#/paramsPassingByRouter/paramsMode/20180101/%E5%BC%A0%E4%B8%89/z3/18
缺點(diǎn):同上。
1 和 2 結(jié)合使用:this.$router.push({ name: '模塊名稱(chēng)', params: { // 各參數(shù) }, query: { // 各參數(shù) } })。
老老實(shí)實(shí)的用 localStorage 存儲(chǔ)。
url: http://localhost:8081/#/paramsPassingByRouter/paramsMode/z3
可以與 params 和 query 方式配合使用,可以暴露的參數(shù)顯示在 url 上,同時(shí)刷新參數(shù)也不會(huì)丟失。
銷(xiāo)毀頁(yè)面的時(shí)候把 localStorage 存儲(chǔ)的內(nèi)容清除。
// router.js { path: 'paramsMode/:aka', name: 'paramsMode', component: ParamsMode } <!-- ParamsMode.vue 修改 --> <script> export default { data () { return { testData: {} } }, created () { const tempData = localStorage.getItem('tempData') if (tempData) { this.testData = JSON.parse(tempData) } else { this.testData = this.$route.params localStorage.setItem('tempData', JSON.stringify(this.$route.params)) } }, beforeDestroy () { localStorage.removeItem('tempData') } } </script>
到此這篇關(guān)于Vue router傳遞參數(shù)并解決刷新頁(yè)面參數(shù)丟失問(wèn)題的文章就介紹到這了,更多相關(guān)Vue router傳遞參數(shù)丟失內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue組件實(shí)現(xiàn)評(píng)論區(qū)功能
這篇文章主要為大家詳細(xì)介紹了Vue組件實(shí)現(xiàn)評(píng)論區(qū)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Vue computed 計(jì)算屬性代碼實(shí)例
在本篇文章里小編給大家分享的是關(guān)于Vue computed 計(jì)算屬性代碼實(shí)例,需要的朋友們可以參考下。2020-04-04vue實(shí)現(xiàn)數(shù)字+英文字母組合鍵盤(pán)功能
這篇文章主要介紹了vue實(shí)現(xiàn)數(shù)字+英文字母組合鍵盤(pán)功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12vue打包上傳服務(wù)器刷新404問(wèn)題的兩種方案
這篇文章主要給大家介紹了關(guān)于vue打包上傳服務(wù)器刷新404問(wèn)題的兩種方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04詳解VUE里子組件如何獲取父組件動(dòng)態(tài)變化的值
這篇文章主要介紹了詳解VUE里子組件如何獲取父組件動(dòng)態(tài)變化的值,子組件通過(guò)props獲取父組件傳過(guò)來(lái)的數(shù)據(jù),子組件存在操作傳過(guò)來(lái)的數(shù)據(jù)并且傳遞給父組件,需要的朋友可以參考下2018-12-12使用VUE+SpringBoot+EasyExcel?整合導(dǎo)入導(dǎo)出數(shù)據(jù)的教程詳解
這篇文章主要介紹了使用VUE+SpringBoot+EasyExcel?整合導(dǎo)入導(dǎo)出數(shù)據(jù)的過(guò)程詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05vue-router路由參數(shù)刷新消失的問(wèn)題解決方法
本篇文章主要介紹了vue-router路由參數(shù)刷新消失的問(wèn)題解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06vue項(xiàng)目如何刷新當(dāng)前頁(yè)面的方法
這篇文章主要介紹了vue項(xiàng)目如何刷新當(dāng)前頁(yè)面的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05