vue中页面自动刷新

当咱们在作项目时,咱们须要作当前页面的刷新来达到数据更新的目的,在此大概总结了几种经常使用的页面刷新的方法。vue

 

1.window.location.reload(),是原生JS提供的方法,app

this.$router.go(0):是vue路由里面的一种方法,ide

这两种方法均可以达到页面刷新的目的,简单粗暴,可是用户体验很差,至关于按F5刷新页面,会有短暂的白屏,至关于页面的从新载入。this

2.经过路由跳转的方法刷新,具体思路是点击按钮跳转一个空白页,而后再立刻跳回来(我的感受比较麻烦,不实用)spa

3.控制<router-view></router-view>的显示与否,在全局组件注册一个方法,该方法控制router-view的显示与否,在子组件调用便可:code

provide / inject 组合 方式感受比较好用,相对的比前面几种方法好用router

(1)先修改App.vueblog

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

(2) 当前须要刷新的页面ip

<template>
    <div>
    </div>
</template>
 
<script>
    export default{
        inject: ['reload'], // 引入方法
        data(){
            return{
            }
        },
        mounted(){
       this.reload() // 哪里须要在哪里调用 }, methods:{
} } </script>
相关文章
相关标签/搜索