在父组件App.vue中声明
<template>
<RouterView v-if="isRouterAlive" />
</template>
<script setup>
import { provide, ref, nextTick } from 'vue';
const isRouterAlive = ref(true)
const reload = () => {
isRouterAlive.value = false
nextTick(() => {
isRouterAlive.value = true
})
}
provide('reload', reload)
</script>
<style lang="scss">
</style>
在子组件文件中引入。
import { inject } from 'vue';
const reload = inject('reload')
本文介绍了一种在Vue应用中实现路由视图强制刷新的方法。通过在App.vue中控制RouterView的显示状态,配合使用nextTick确保DOM更新完成,从而达到刷新当前路由视图的效果。此外,还展示了如何通过provide/inject在父子组件间传递刷新方法。

394

被折叠的 条评论
为什么被折叠?



