主要思想就是使用v-if
销毁和重新渲染组件,达到强制刷新组件的目的。
<template>
<div id="App">
<router-view v-if="isRouterAlive"/>
</div>
</template>
<script>
export default {
name: 'App',
data () {
return {
isRouterAlive: true
},
// 把刷新组件的方法传给子组件
provide () {
return {
reload: this.reload
}
},
methods: {
reload () { // 调用该方法可刷新组件
this.isRouterAlive = false
this.$nextTick(() => { //$nextTick() 方法传入一个回调函数,在下一次渲染页面时调用该回调
this.isRouterAlive = true
})
}
}
}
</script>