1. 程式人生 > 其它 >vue中自動重新整理頁面

vue中自動重新整理頁面

vue如今是十分常用的前端框架,而我們在使用vue框架時總會碰到一個問題,那就是進行完所需要的操作以後如何進行頁面自動重新整理,以下就來說一說幾種方法:
一. window.location.reload(),是原生JS提供的方法,

this.$router.go(0):是vue路由裡面的一種方法,
這兩種方法簡單粗暴,不過有一個缺點就是,它們相當於整個頁面重新重新整理,這樣便會出現幾秒的空白頁,可能會使使用者感受不加。

二.控制的顯示與否,在全域性元件註冊一個方法,該方法控制router-view的顯示與否,在子元件呼叫即可:
(1)先修改App.vue

<template>
<div id="app">
<router-view v-if="isRouterAlive"></router-view>
</div>
</template>

<script>
export default {
name: 'App',
provide() { // 註冊一個方法
return {
reload: this.reload
}
},
data() {
return {
isRouterAlive: true
}
},
methods: {
reload() {
this.isRouterAlive = false
this.$nextTick(function() {
this.isRouterAlive = true
})
}
}
}
</script>

在這裡我們需要注意,有些App.vue頁面中已有<router-view v-if="isRouterAlive"></router-view>
那便不需要重複宣告。

(2) 在當前需要重新整理的頁面

<template>
<div>
</div>
</template>

<script>
export default{
inject: ['reload'], // 引入方法
data(){
return{
}
},
mounted(){
       this.reload() // 哪裡需要在哪裡呼叫
},
methods:{}
}
</script>