方法是從別人那看來的,記錄一下。 原文鏈接忘了,所以發原創 了。
vue通過js執行完方法後有時需要刷新當前路由,常見的this.reload(), window.reload()之類,但這類刷新會刷新整個頁面,出現白屏,體驗不好,所以在網上又找了一種中轉的方法,如下。
需要刷新路由的頁面調用刷新方法
新建refresh.vue
<!-- 空頁面,負責中轉到目標頁面 -->
<template>
<div></div>
</template>
<script>
export default {
name: 'refresh',
data () {
return {
}
},
beforeRouteEnter (to, from, next) {
next(vm => {
vm.$router.replace(from.path)
})
}
}
</script>
<style scoped>
</style>
from.path爲需要刷新頁面的地址。
爲什麼要用replace而不用push呢,因爲push假如刷新成功後點返回會回到refresh頁面,replace可以避免這個問題。