vue中手动设置刷新当前页面

vue-router重新路由到当前页面,或者$router.push页面不刷新

 

方法:

1.  window.reload(),或   router.go(0)    ,js原生方法,只要停留在本页面就会不停刷新(不建议使用)

2. 全局声明reload方法,控制router-view的显示或隐藏,从而控制页面的再次加载

    (允许一个祖先组件向其所有子孙后代注入一个依赖,不论组件层次有多深,并在起上下游关系成立的时间里始终生效。)

 

     根组件APP.vue中声明方法:

     

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

 应用页面引用:(注意要跳转的页面也要inject注册)

inject: ['reload']   //注册
this.reload()  //使用

       

 

 

 

posted on 2021-06-26 17:59  一名小学生呀  阅读(469)  评论(0编辑  收藏  举报

导航