詳解vue幾種主動刷新的方法總結(jié)
當(dāng)我們在做項(xiàng)目時,我們需要做當(dāng)前頁面的刷新來達(dá)到數(shù)據(jù)更新的目的,在此我們大概總結(jié)了幾種常用的頁面刷新的方法。
1.window.location.reload(),是原生JS提供的方法,this.$router.go(0):是vue路由里面的一種方法,這兩種方法都可以達(dá)到頁面刷新的目的,簡單粗暴,但是用戶體驗(yàn)不好,相當(dāng)于按F5刷新頁面,會有短暫的白屏,相當(dāng)于頁面的重新載入。
2.通過路由跳轉(zhuǎn)的方法刷新,具體思路是點(diǎn)擊按鈕跳轉(zhuǎn)一個空白頁,然后再馬上跳回來:
當(dāng)前頁面:
<template>
<div>
<el-button type="primary" class="btn" @click="btnaction">摁我刷新頁面</el-button>
</div>
</template>
<script>
export default{
data(){
return{
}
},
mounted(){
},
methods:{
btnaction() {
// location.reload()
// this.$router.go(0)
this.$router.replace({
path:'/empty',
name:'empty'
})
}
}
}
</script>
空白頁面:
<template>
<h1>
空頁面
</h1>
</template>
<script>
export default{
data() {
return{
}
},
created(){
this.$router.replace({
path:'/',
name:'father'
})
}
}
</script>
當(dāng)點(diǎn)擊按鈕時地址欄會有快速的地址切換過程。
3.控制<router-view></router-view>的顯示與否,在全局組件注冊一個方法,該方法控制router-view的顯示與否,在子組件調(diào)用即可:
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
console.log('reload')
})
}
}
}
</script>
當(dāng)前組件:
<template>
<div>
<el-button type="primary" class="btn" @click="btnaction">摁我刷新頁面</el-button>
</div>
</template>
<script>
export default{
inject: ['reload'], // 引入方法
data(){
return{
}
},
components:{
},
mounted(){
},
methods:{
btnaction() {
// location.reload()
// this.$router.go(0)
// this.$router.replace({
// path:'/empty',
// name:'empty'
// })
this.reload() // 調(diào)用方法
}
}
}
</script>
當(dāng)點(diǎn)擊按鈕時所有頁面重新渲染。
4.對列表操作后的表格刷新的操作方法:
當(dāng)我們在操作數(shù)據(jù)表格時,會對表格進(jìn)行增刪改查,做完操作我們需要對列表進(jìn)行刷新來達(dá)到重新渲染,但是當(dāng)如果存在分頁,我們在比如第3頁進(jìn)行刪除操作,如果按照以往的刷新方法,刷新完后便進(jìn)入了第一頁,這肯定不是我們想要的,這時候我們常用的做法是重新調(diào)用數(shù)據(jù)渲染方法,通常我們會有獲取數(shù)據(jù)接口,刪除接口等等,頁面加載時調(diào)用獲取數(shù)據(jù)的方法,當(dāng)我們執(zhí)行刪除操作時,再重新調(diào)用獲取數(shù)據(jù)的方法即可。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決Vue.js父組件$on無法監(jiān)聽子組件$emit觸發(fā)事件的問題
今天小編就為大家分享一篇解決Vue.js父組件$on無法監(jiān)聽子組件$emit觸發(fā)事件的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue實(shí)現(xiàn)選項(xiàng)卡及選項(xiàng)卡切換效果
這篇文章主要介紹了vue實(shí)現(xiàn)選項(xiàng)卡選項(xiàng)卡切換效果,這里的Vue以單文件的形式引入,另外代碼在實(shí)現(xiàn)上會一步步的進(jìn)行優(yōu)化。需要的朋友可以參考下2018-04-04
Vue中使用better-scroll實(shí)現(xiàn)輪播圖組件
better-scroll 是一款重點(diǎn)解決移動端(已支持 PC)各種滾動場景需求的插件。這篇文章主要介紹了Vue中使用better-scroll實(shí)現(xiàn)輪播圖組件的實(shí)例代碼,需要的朋友可以參考下2020-03-03
Vue 3 自定義權(quán)限指令 v-action的作用
在實(shí)際的前端開發(fā)中,尤其是涉及到權(quán)限管理的系統(tǒng),我們經(jīng)常需要根據(jù)用戶的權(quán)限動態(tài)控制某些按鈕的顯示和隱藏,這篇文章主要介紹了Vue 3 自定義權(quán)限指令 v-action的相關(guān)知識,需要的朋友可以參考下2025-04-04

