1. 程式人生 > >Vue用router.push(傳參)跳轉頁面,參數改變,跳轉頁面數據不刷新的解決辦法

Vue用router.push(傳參)跳轉頁面,參數改變,跳轉頁面數據不刷新的解決辦法

ren osi pat 出現 響應 router 手機 dep code

vue-router同路由$router.push不跳轉一個簡單解決方案

vue-router跳轉一般是這麽寫:


goPage(ParentDeptCode2,DeptCode2,hosName,hosId){
this.$router.push({
path:‘/ChoiceTime‘,
query:{
DeptCode:ParentDeptCode2,
DeptCode2:DeptCode2,
hosName:hosName,
hosId:hosId
}
})
}

但是當遇到,需要跳轉同頁面不同query的情況,上面的方法不起作用。基本頁面所有組件都需要刷新,我們只要跳轉加載,

$route 作為vue實例的一個響應式屬性,和在data中寫的屬性本質上是一樣的,都可以通過this的方式拿到。既然你可以監聽data中的屬性變化,同樣也可以監聽 $route 的變化。watch中監聽的對象默認回調函數中的參數值就是newVal,oldVal。作為 $route 屬性來說當然也就是 to 和 from 的概念了。

watch: {
    ‘$route‘ (to, from) {
        this.$router.go(0);
    }
},

但是這種情況會出現手機端的白屏情況,而且對應移動端的ios依舊解決不了router.push(傳參)跳轉頁面,參數改變,跳轉頁面數據不刷新的解決辦法 .

所以,我們需要在定義路由界面這樣寫

app.vue
<keep-alive v-if="$route.meta.keepAlive">
<router-view/>
</keep-alive>
<!--<FooterGuide />-->
<router-view v-if="!$route.meta.keepAlive">
<!-- 這裏是不被緩存的視圖組件,比如 page3 -->
</router-view>
router/index.js
{ name:
‘ChoiceTime‘, path:
‘/ChoiceTime/:DeptCode/:DeptCode2/:hosName/:hosId‘, component: ChoiceTime, meta: { title: ‘選擇時間‘, keepAlive: false, }, }, 路由將跳轉到ChoiceTime.vue頁面
goPage(ParentDeptCode2,DeptCode2,hosName,hosId){
this.$router.push({
name:‘ChoiceTime‘,
params:{
DeptCode:ParentDeptCode2,
DeptCode2:DeptCode2,
hosName:hosName,
hosId:hosId
}
})
}
watch: {
‘$route‘ (to, from) {
this.$router.go(0);
}
},

這樣,完美解決了移動端的頁面刷新問題,也不會出現白屏的問題.

Vue用router.push(傳參)跳轉頁面,參數改變,跳轉頁面數據不刷新的解決辦法