美文网首页
Vue3使用provide和inject对页面进行刷新

Vue3使用provide和inject对页面进行刷新

作者: charmingcheng | 来源:发表于2022-11-01 15:12 被阅读0次

首先,需要修改一下app.vue文件,通过reload方法来控制router-view的显示或者隐藏

<template>
    <router-view v-if="isRouterAlive" />
</template>

<script lang="ts" setup>
import {provide, ref, nextTick} from 'vue'
const isRouterAlive = ref(true)
const reload = () => {
    isRouterAlive.value = false
    void nextTick(() => {
        isRouterAlive.value = true
    })
}
provide('reload', reload)
</script>

在子页面中使用inject调用provide的方法

<script lang="ts" setup>
import {inject} from 'vue'

const refresh = inject('reload')
const handle = async () => {
  await ...
  refresh()
}
</script>

相关文章

网友评论

      本文标题:Vue3使用provide和inject对页面进行刷新

      本文链接:https://www.haomeiwen.com/subject/rkgrtdtx.html