Vue项目路由刷新

当vue单页面需要替换参数并刷新页面时,这个时候使用this.$router.pushthis.$router.replace会发现路由改变了,但是页面上的数据并没有实时刷新。在网上找到了以下几种方法,亲测可用:

this.$router.go(0)

在具体页面中,先通过this.$router.pushthis.$router.replace替换路由,随后调用this.$router.go(0),页面就会强制刷新,但是该强制刷新与F5刷新效果类似,页面会有空白时间,体验感不好;

provide/inject

首先在app.vue页面中增加如下配置:




然后在具体页面中加上inject配置,具体如下:

 export default {
    name: 'orderAndRandom',
    
    // 就是下面这行
    inject: ['reload'],
    data() {},
    
    // 省略
 }

加上配置后,在调用this.$router.pushthis.$router.replace替换路由后,再新增this.reload()就可以实现页面内数据刷新。

你可能感兴趣的:(Vue项目路由刷新)