前端刷新页面的方法

reload() 方法

reload() 方法用于刷新当前文档,类似于浏览器上的刷新页面按钮

window.location.reload()

replace() 方法

replace() 方法可用一个新文档取代当前文档

window.location.replace("http://www.runoob.com")

页面自动刷新

其中5指每隔5秒刷新一次页面

<meta http-equiv="refresh" content="5">

Vue-Router路由跳转

this.$router.go(0)

Vue依赖注入

<template>
    <div id="app">
    	<router-view v-if="isRouterAlive"></router-view>
	</div>
</template>
<script>
    export default {
        name: 'App',
        // 父组件中通过provide来提供变量,在子组件中通过inject来注入变量
        provide () {                                             
            return {
                reload: this.reload                                              
            }
        },
        data() {
            return{
                //控制视图是否显示的变量
                isRouterAlive: true                    
            }
        },
        methods: {
            reload () {
                //先关闭
                this.isRouterAlive = false
                this.$nextTick(function () {
                    //再打开
                    this.isRouterAlive = true    
                }) 
            }
        }}
</script>

需要刷新页面的组件中注入App里的reload方法

export default {
	inject:['reload'],                            
    data () {
        return {}
	}
}

在需要刷新页面的代码块中使用

this.reload()

参考文章

Vue刷新页面的三种方式

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐