Vue實現(xiàn)當(dāng)前頁面刷新的4種方法舉例
前言
這兩周在寫一個后臺管理,每次調(diào)用接口實現(xiàn)增刪改查的過程中,都需要刷新當(dāng)前頁面或者刷新數(shù)據(jù)。如果手動點擊瀏覽器的小圈圈不僅麻煩、用戶體驗感極差,而且不會真的有人讓用戶手動刷新叭。。。這個問題可以稱得上是前端的bug了。那么,順著這個問題,一通搜尋下來,整理了幾個刷新當(dāng)前頁面的方法,如下:
方法一:location.reload
學(xué)習(xí)JS的過程中,大家應(yīng)該都了解過Browser 對象,其中Location 對象是 window 對象的一部分。Location 對象中有一個方法,也就是reload()方法,用于刷新當(dāng)前文檔,類似于瀏覽器上的刷新頁面按鈕。
代碼測試:
<template>
<div class="hello">
<img src="../imgs/01.jpg" alt="" />
<button @click="refresh">點擊刷新頁面</button>
</div>
</template>
<script>
export default {
name: "HelloWorld",
methods: {
refresh() {
location.reload();
},
},
};
</script>
<style scoped>
.hello img {
width: 800px;
display: block;
margin-bottom: 20px;
}
</style>
效果展示:

缺點: 想必大家都能看出來了叭,一閃一閃亮晶晶~
方法二:$router.go(0)
這種方法大家應(yīng)該比較熟悉了,學(xué)過vue路由跳轉(zhuǎn)的都知道$router.go()的作用:
> this.$router.go(-1):后退+刷新; > this.$router.go(0):刷新; > this.$router.go(n) :前進n個頁面
這個方法等同于上面的location.reload,也是利用瀏覽器的刷新功能,瘋狂按F5刷新。。。
代碼測試:
<template>
<div class="hello">
<img src="../imgs/02.jpg" alt="" />
<button @click="refresh">點擊刷新頁面</button>
</div>
</template>
<script>
export default {
name: "HelloWorld",
methods: {
refresh() {
this.$router.go(0);
},
},
};
</script>
<style scoped>
.hello img {
width: 800px;
display: block;
margin-bottom: 20px;
}
</style>
效果展示:

缺點: 肉眼可見!會出現(xiàn)一瞬間的空白頁面,用戶體驗不好
方法三:provide、inject和$nextTick
首先,我們來認識一下這組選項:
provide 選項應(yīng)該是:一個對象或返回一個對象的函數(shù)。
inject 選項應(yīng)該是:一個字符串?dāng)?shù)組,或 一個對象,對象的 [key] 是本地的綁定名。
在學(xué)習(xí)vue父子組件通信的時候,大家應(yīng)該都知道這是用來干嘛的了:父組件通過provide向子組件傳遞數(shù)據(jù),子組件通過inject獲取數(shù)據(jù)。
那么$nextTick又是干哈的呢?
$nextTick 又說是Vue的另一個生命周期函數(shù):當(dāng)你修改完數(shù)據(jù)(數(shù)據(jù)更新了)之后,Vue幫你操作完DOM之后,把真實的DOM放入頁面了(Dom更新渲染),Vue再幫我們調(diào)用這個函數(shù)(可以監(jiān)聽DOM元素被修改后,在該函數(shù)中寫你要執(zhí)行的邏輯)。
接下來,我們來組合一下思路:
我們在父組件中通過給<router-view></router-view>添加v-if來控制子組件銷毀和重建的方式,從而控制頁面的再次加載。然后在需要當(dāng)前頁面刷新的頁面中注入 reload 依賴,直接通過this.reload來調(diào)用刷新。
代碼測試:
App組件:
<template>
<div id="app">
<HelloWorld v-if="isReload" />
</div>
</template>
<script>
import HelloWorld from "./components/HelloWorld.vue";
export default {
name: "App",
data() {
return {
isReload: true,
};
},
components: {
HelloWorld,
},
provide() {
return {
msg: "未刷新",
reload: this.reload,
};
},
methods: {
async reload() {
this.isReload = false;
await this.$nextTick();
this.isReload = true;
},
},
};
</script>
子組件:
<template>
<div class="hello">
<img src="../imgs/03.jpg" alt="" />
<p>{{ msg }}</p>
<button @click="refresh">點擊刷新頁面</button>
</div>
</template>
<script>
export default {
inject: ["reload", "msg"],
name: "HelloWorld",
methods: {
refresh() {
this.msg = "我刷新啦!";
this.reload;
},
},
};
</script>
<style scoped>
.hello img {
width: 800px;
display: block;
margin-bottom: 20px;
}
</style>效果展示:

缺點: 可以看到頁面不會刷白,但是這種方法也有很多弊端。我們都知道Vue 在修改數(shù)據(jù)后,視圖不會立刻更新,而是等同一事件循環(huán)中的所有數(shù)據(jù)變化完成之后,再統(tǒng)一進行視圖更新。這樣容易造成事件循環(huán);并且使用provide和inject也涉及到組件的多層級通信,有些繁瑣。
方法四:創(chuàng)建空白頁
這個方法…我此前從沒用過,就是利用$router.replace路由跳轉(zhuǎn)到一個空白頁面,然后在空白頁面中立即執(zhí)行$router.replace切換到原來的頁面。$router.replace不會向 history 添加新紀錄,當(dāng)路由跳轉(zhuǎn)得比較快的時候,不會出現(xiàn)一瞬間的空白頁。
代碼測試:
空白頁:
<template>
<div class="hello"></div>
</template>
<script>
export default {
name: "HelloTest",
created() {
this.$router.replace(this.$route.query.redirect);
},
};
</script>
<style scoped>
</style>
需要刷新的頁面:
<template>
<div class="hello">
<img src="../imgs/04.jpg" alt="" />
<button @click="refresh">點擊刷新頁面</button>
</div>
</template>
<script>
export default {
name: "HelloWorld",
methods: {
refresh() {
this.$router.replace(`/blank?redirect=${this.$route.fullPath}`);
},
},
};
</script>
<style scoped>
.hello img {
width: 800px;
display: block;
margin-bottom: 20px;
}
</style>路由:
const router = new VueRouter({
mode: 'history',
routes: [{
path: "/",
component: () => import('../components/HelloWorld.vue'),
meta: {
keepAlive: true,
}
},
{
path: "/blank",
component: () => import('../components/HelloTest.vue'),
meta: {
keepAlive: true,
}
}]
})
效果展示:

缺點: 大家應(yīng)該可以看到地址欄的變化。。
總結(jié)
以上就是比較常見的當(dāng)前頁面刷新的方法,各有優(yōu)缺點,根據(jù)應(yīng)用場景使用。
到此這篇關(guān)于Vue實現(xiàn)當(dāng)前頁面刷新的4種方法的文章就介紹到這了,更多相關(guān)Vue當(dāng)前頁面刷新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue-cli項目獲取本地json文件數(shù)據(jù)的實例
下面小編就為大家分享一篇Vue-cli項目獲取本地json文件數(shù)據(jù)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
vue flex 布局實現(xiàn)div均分自動換行的示例代碼
這篇文章主要介紹了vue flex 布局實現(xiàn)div均分自動換行,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
watch(監(jiān)視屬性)和computed(計算屬性)的區(qū)別及實現(xiàn)案例
watch和computed是vue實例對象中的兩個重要屬性,watch是監(jiān)視屬性,用來監(jiān)視vue實例對象上屬性和方法的變化,computed被稱為計算屬性,可以將data對象中的屬性進行計算得到新的屬性,這篇文章主要介紹了watch(監(jiān)視屬性)和computed(計算屬性)的區(qū)別,需要的朋友可以參考下2023-05-05
Vue3使用defineAsyncComponent實現(xiàn)異步組件加載的代碼示例
在 Vue 3 中,異步組件加載是一種優(yōu)化應(yīng)用性能的重要手段,通過異步加載組件,可以減少初始加載時的資源體積,從而提升應(yīng)用的加載速度和用戶體驗,本文將詳細介紹如何使用 defineAsyncComponent 實現(xiàn)異步組件加載,并提供相關(guān)的代碼示例,需要的朋友可以參考下2025-03-03

