---- AI试用 ---域名问题某些图片和js资源无法访问,导致一些代码实例无法运行!(代码里gzui.net换成momen.vip即可)

vue 刷新当前页面或者跳转页面时候刷新

前端开发 蚂蚁 601℃ 0评论

原文
1 用vue-router 重新路由的时候到当前页面的时候是不进行刷新的
2 采用window.reload() 或者router.go(0) hisory.go(0) 刷新的时候整个浏览器进行刷新加载, 但是页面闪烁, 体验不好

解决方法

比较终极解决办法, 页面闪烁是不存在的, 几乎没有啥毛病。
provide /inject 组合

作用是: 允许一个祖先组件向其所有子孙后代注入一个依赖, 无论组件层次有多深,并在其上下游关系成立时间里始终生效。

App.vue 文件中 修改文件 整个配置如此:

<template>
  <div id="app">
    <router-view v-if="isRouterAlive"/>
  </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>

在页面中应用:

inject: ['reload'],

this.reload();

转载请注明:有爱前端 » vue 刷新当前页面或者跳转页面时候刷新

喜欢 (2)or分享 (0)