vue hash模式下路由改变页面不刷新问题

21 篇文章 0 订阅

//js原始方法刷新,相当于按F5刷新页面,会有短暂的白屏,相当于页面的重新载入//js原始方法刷新,相当于按F5刷新页面,会有短暂的白屏,相当于页面的重新载入

hash模式下路由改变页面不刷新问题

在App.vue文件中添加:

export default {
  mounted () {
    // 检测浏览器路由改变页面不刷新问题,hash模式的工作原理是hashchange事件
    window.addEventListener('hashchange', () => {
      console.log(window.location.hash,'window.location.hash')
      let currentPath = window.location.hash.slice(1)
      console.log(currentPath,'currentPath')
      if (this.$route.path !== currentPath) {
        this.$router.push(currentPath)
      }
    }, false)
  },
}

vue页面刷新的6种方式


1、this.$forceUpdate()  //强制渲染页面

2、window.location.reload() //js原始方法刷新,相当于按F5刷新页面,会有短暂的白屏,相当于页面的重新载入
 3、this.$router.go(0)   //Vue自带的路由进行跳转,相当于按F5刷新页面,会有短暂的白屏,相当于页面的重新载入

4、通过inject

<template>
  <div>
      <div class="header">
          <button @click="update()">刷新页面</button>
      </div>
  </div>
</template>

<script>
export default {
data(){
  return{

  }
},
inject:[
  'reload'
],
methods:{
  update(){
    this.reload()
    console.log('刷新页面')
  }
}
}
</script>

4、通过路由跳转的方法刷新,具体思路是点击按钮跳转一个空白页,然后再马上跳回来

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

<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
        console.log('reload')
      })
    }
  }
}
</script>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值