<router-view v-if=“isRouterAlive“> 刷新当前页面 - Vue2

vue刷新当前页面有挺多种方法,比如

window.location.reload() 或者 this.$router.go(0)

但是这两种方法是会出现一瞬间的白屏,体验不好,所以这里给大家推荐第三种比较好用的刷新页面的方法

在app.vue的<router-view></router-view>加上v-if属性

<router-view v-if="isRouterAlive"></router-view>

在data里面加上isRouterAlive,当然这个属性名可以自己定义,默认值为true

  data () {
      return {
        isRouterAlive: true
      }
  }

methods里面加入一个刷新的方法

methods: {
    reload () {
      this.isRouterAlive = false
      this.$nextTick(function() {
         this.isRouterAlive = true
      })
    }
  }

最后,需要把这个函数 provide 出去

provide () {
    return {
      reload: this.reload
    }
  }

这样,app.vue上就设置完了

那么当我们需要刷新的时候,在需要的页面上加上这个函数就可以了

首先注入这个函数

inject: ['reload']

然后在需要用到这个函数的地方去引用就行了

refresh () {
  this.reload()
}

这样子就可以刷新页面了,而且不会出现白屏的情况,比前面两种方法好用,推荐大家使用。

附带上完整代码

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

<style>
  #app{
    position: relative;
  }
  @media only screen and (min-width: 1200px) {
    .wrap{
      width: 65%;
      margin: 0 auto;
    }
  }
  
</style>

<template>
    <button @click="refresh"></button>
</template>
<script>
    export default{
        name: 'refresh',
        inject: ['reload'],
        methods: {
              refresh () {
                  this.reload()
              }
        }
    }
</script>
  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据您提供的代码片段,您想要实现的是根据 `$route.meta.keepAlive` 的值来决定是否缓存 `router-view` 组件。在您的修改中,您想要在 `$route.meta.keepAlive` 为 `true` 时才缓存 `router-view` 组件。 根据您的描述,缓存似乎没有起作用。有几个可能的原因导致缓存失效: 1. `$route.meta.keepAlive` 的值没有正确设置为 `true`。请确保在路由配置中正确设置了 `meta` 字段,以及在对应的路由对象中将 `keepAlive` 设置为 `true`。 2. 使用了相同的 `:key`。在您的代码中,`<router-view>` 组件和 `<keep-alive>` 组件都使用了 `$route.path` 作为 `:key`。这会导致两个组件具有相同的 `:key`,从而导致缓存失效。您可以尝试将 `<keep-alive>` 组件的 `:key` 设置为不同的值,例如使用一个固定的字符串。 下面是您修改后的代码片段,我对其中的一些地方进行了注释,希望能帮助您解决问题: ```html <div class="router" :data-keepAlive="$route.meta.keepAlive"> <breadcrumb class="breadcrumb" /> <!-- 取消下面的注释,并删除上面的 <keep-alive> 组件 --> <!-- <keep-alive> <router-view :key="'cached-' + $route.path"></router-view> </keep-alive> --> <!-- 取消下面的注释,并删除上面的 <router-view> 组件 --> <!-- <keep-alive v-if="$route.meta.keepAlive"> <router-view :key="'cached-' + $route.path"></router-view> </keep-alive> --> <!-- 取消下面的注释,并删除上面的 <router-view> 组件 --> <!-- <router-view :key="'non-cached-' + $route.path"></router-view> --> </div> ``` 请注意,根据您的实际需求,您可能需要根据不同的情况来使用不同的缓存策略。以上代码片段提供了一种思路,但具体实现可能需要根据您的项目结构和需求进行调整。希望这能帮助到您!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值