Vue 项目功能实现:刷新当前页面

栏目: JavaScript · 发布时间: 6年前

内容简介:前些日子项目中突然接到了一个需求,要求点击当前路由刷新页面,进过实验有如下几种方案可实现需求,并简述不同。此方式是利用了 history 中前进和后退的功能,传入 0 刷新当前页面。缺点:页面整个刷新,会白屏。

前言

前些日子项目中突然接到了一个需求,要求点击当前路由刷新页面,进过实验有如下几种方案可实现需求,并简述不同。

1. this.$router.go(0)

此方式是利用了 history 中前进和后退的功能,传入 0 刷新当前页面。

缺点:页面整个刷新,会白屏。

2. location.reload()

直接使用刷新当前页面的方法。

缺点:同 this.$router.go(0) 一样,会白屏。

3. 给 router-view 标签添加 v-if

通过 $nextTick() ,协助实现。先把 <router-view /> 隐藏,隐藏后再让它显示,达到刷新当前页面的功能。是目前最合适的实现方式。

<!-- html -->
<router-link :to="url" @click.native="refreshView">页面</router-link>
<router-view v-if="showView"/>

<!-- js -->
<script>
export default {
  data () {
    return {
      showView: true // 用于点击当前页的router时,刷新当前页
    }
  },
  methods: {
    refreshView (url) {
      this.showView = false // 通过v-if移除router-view节点
      this.$nextTick(() => {
        this.showView = true // DOM更新后再通过v-if添加router-view节点
      })
    }
  }
}
</script>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Artificial Intelligence

Artificial Intelligence

Stuart Russell、Peter Norvig / Pearson / 2009-12-11 / USD 195.00

The long-anticipated revision of this #1 selling book offers the most comprehensive, state of the art introduction to the theory and practice of artificial intelligence for modern applications. Intell......一起来看看 《Artificial Intelligence》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

SHA 加密
SHA 加密

SHA 加密工具