Vue刷新重載當前頁面

許多場景需要做到當前頁面刷新,但直接用router.push當前頁面是沒辦法刷新的,硬刷新重載頁面體驗很差,在使用vue-element-admin時,看到了他們的實現(xiàn)方式redirect頁面在created里收集到上一頁面(需要刷新的頁面)的路徑和參數(shù)信息,再返回到上一頁面,實現(xiàn)刷新

1.創(chuàng)建頁面

views/redirect文件夾下新建 index.vue
路徑@/views/redirect/index.vue
代碼如下:

<script>
export default {
  created() {
    const { params, query } = this.$route
    const { path } = params
    this.$router.replace({ path: '/' + path, query })
  },
  render: function(h) {
    return h() // avoid warning message
  }
}
</script>

2.配置路由

// router.js
const router = new Router({
  ...
  routes: [
    ...
    {
      path: "/redirect",
      component: Layout,
      hidden: true,
      children: [{
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index")
      }]
    }
    ...
  ]
  ...
})

3.使用

...
// 刷新
this.$router.replace({ path: `/redirect${this.$route.fullPath}` })
...
?著作權(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)容