Zlog
首页时间轴关于
Zlog

一个探索技术、编程和构建 Web 的个人空间。

© 2026 Zlog

导航

首页时间轴关于

链接

首页/Vue刷新页面的三种方式

Vue刷新页面的三种方式

location.reload();

Z

Zephyr110

2021年2月1日·1 分钟
|
frontendfrontend-frameworkfrontend-vue

1. 原始方法

location.reload();

2. Vue 自带的路由跳转

this.$router.go(0);

以上两种刷新方法会造成页面短暂闪烁,交互体验不好,所以可以采用下面的方法控制router-view的显示与否

3. 注册全局方法

在 APP 里注册如下方法

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

然后在需要刷新的页面组件中inject注入并调用

js
export default {
    inject:['reload'],          //注入App里的reload方法
    data () {
        return {
    	.......
        }
    },
    methods: reloadFn(){       //   需要刷新的的代码块中调用reload方法
        this.reload();
    }

标签

frontendfrontend-frameworkfrontend-vue

评论

相关文章

Facade

外观模式:就是提供一个统一的接口去访问多个子系统的多个不同的接口,为子系统中的一组接口提供统一的高层接口。使得子系统更容易使用,不仅简化类中的接口,而且实现调用者和接口的解耦。

2021年12月26日·2 分钟

Singleton

单例模式:就是在整个运行时域,一个类只有一个实例对象。

2021年12月26日·2 分钟

Function Overloading

函数名相同,函数的参数列表不同(参数个数、参数类型),根据参数的不同之行不同的操作。

2021年12月19日·2 分钟

← 返回文章列表