Vue頁面刷新方法

問題描述

  • 今天在做畢業(yè)設(shè)計時候遇到一個問題, 當在搜索框重復搜索時,下面顯示的搜索結(jié)果必須要手動刷新后才可以更新, 想實現(xiàn)的效果是,點擊搜索后最新的頁面自動渲染。

問題解決

方法一

location.reload()
  • 缺點: 頁面閃爍,可以看到空白頁

方法二

this.$router.go(0)
  • 缺點用上

方法三

數(shù)據(jù)更新以后, 先跳轉(zhuǎn)到假路由,再重新跳轉(zhuǎn)回頁面

let NewPage = "_empty" + "?time=" + new Date().getTime() / 500;
this.$router.push(NewPage);
this.$router.go(-1);

方法四

-provide/ inject 方法
-在App.vue里聲明reload方法, v-if 來控制 router-view 的顯示或隱藏來控制頁面的再次加載

// 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>

在需要刷新的頁面添加, 在操作完成后( 比如刪除,更新, 增加),調(diào)用this.reload()

export default {
   inject: ['reload']
...
   this.reload()
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容