Vue router傳遞參數(shù)并解決刷新頁面參數(shù)丟失問題
Vue Router 傳參方式:
1. this.$router.push({ name: '模塊名稱', 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
頁面顯示:{"id":"20180101","name":"張三","aka":"z3","age":"18"}
但是刷新頁面后,數(shù)據(jù)會丟失,顯示:{}。
2. this.$router.push({ name: '模塊名稱', 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
頁面顯示:{"id":"20180101","name":"張三","aka":"z3","age":"18"}
刷新頁面后,數(shù)據(jù)不會丟失。
解決刷新頁面數(shù)據(jù)丟失的方案:
使用 this.$router.push({ name: '模塊名稱', query: { // 各參數(shù) } }) 方式傳參。
缺點(diǎn):參數(shù)值都拼接在 url 上,url 會很長,同時(shí)都可被看到。
this.$router.push({ name: '模塊名稱', 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: '模塊名稱', params: { // 各參數(shù) }, query: { // 各參數(shù) } })。
老老實(shí)實(shí)的用 localStorage 存儲。
url: http://localhost:8081/#/paramsPassingByRouter/paramsMode/z3
可以與 params 和 query 方式配合使用,可以暴露的參數(shù)顯示在 url 上,同時(shí)刷新參數(shù)也不會丟失。
銷毀頁面的時(shí)候把 localStorage 存儲的內(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ù)并解決刷新頁面參數(shù)丟失問題的文章就介紹到這了,更多相關(guān)Vue router傳遞參數(shù)丟失內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue-resource請求實(shí)現(xiàn)http登錄攔截或者路由攔截的方法
這篇文章主要介紹了vue-resource請求實(shí)現(xiàn)http登錄攔截或者路由攔截的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07
vue中Element-ui 輸入銀行賬號每四位加一個空格的實(shí)現(xiàn)代碼
我們在輸入銀行賬號會設(shè)置每四位添加一個空格,輸入金額,每三位添加一個空格。那么,在vue,element-ui 組件中,如何實(shí)現(xiàn)呢?下面小編給大家?guī)砹藇ue中Element-ui 輸入銀行賬號每四位加一個空格的實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧2018-09-09
Vue mockjs編寫假數(shù)據(jù)并請求獲取實(shí)現(xiàn)流程
這篇文章主要介紹了Vue mockjs編寫假數(shù)據(jù)并請求獲取實(shí)現(xiàn)流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12
解決echarts echarts數(shù)據(jù)動態(tài)更新和dataZoom被重置問題
這篇文章主要介紹了解決echarts echarts數(shù)據(jù)動態(tài)更新和dataZoom被重置問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
vue異步組件與組件懶加載問題(import不能導(dǎo)入變量字符串路徑)
這篇文章主要介紹了vue異步組件與組件懶加載問題(import不能導(dǎo)入變量字符串路徑),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
使用mint-ui開發(fā)項(xiàng)目的一些心得(分享)
下面小編就為大家?guī)硪黄褂胢int-ui開發(fā)項(xiàng)目的一些心得(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09

