vue页面刷新

vue 页面刷新

provide/inject 组合

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<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(() => {
this.isRouterAlive = true;
});
},
},
};
</script>

在需要当前页面刷新的页面注入 App.vue 组件提供(provide)的 reload 依赖,然后 this.reload()调用

1
2
3
4
5
6
7
8
export default {
inject: ["reload"],
methods: {
test() {
this.reload();
},
},
};