I need to click a button to modify the key value of the component through the bound function, and I find that the key value has changed, but the component is not unloaded for re-rendering.
I checked a lot of information on the Internet, found that they are so to achieve page refresh, this problem has been troubled for a long time.
I hope there are kind people to guide me how to correctly use the key attribute to complete the page refresh.
enter image description here
<template>
<!-- 路由组件出口 -->
<!-- v-slot可以获取当前渲染的组件以及路由信息 -->
<router-view v-slot="{ Component }">
<transition name="fade">
<!-- 渲染home一级路由的子路由 -->
<component :is="Component" :key="refreshContent"/>
</transition>
</router-view>
</template>
<script setup lang="ts">
import emitter from "@/utils/emitter";
import { onUnmounted, onMounted } from "vue";
let refreshContent = 0
emitter.on('refresh_content', () => {
refreshContent += 1
console.log(refreshContent)
})
onMounted(() => {
console.log('load')
})
onUnmounted(() => {
emitter.off('refresh_content')
console.log('unload')
})
</script>
<style scoped>
.fade-enter-from {
opacity: 0;
transform: scale(0)
}
.fade-enter-to {
opacity: 1;
transform: scale(1);
}
.fade-enter-active {
transform: all 0.5s;
}
</style>```
I need to click a button to modify the key value of the component through the bound function, and I find that the key value has changed, but the component is not unloaded for re-rendering.
I checked a lot of information on the Internet, found that they are so to achieve page refresh, this problem has been troubled for a long time.
I hope there are kind people to guide me how to correctly use the key attribute to complete the page refresh.
3
You need to set refreshContent to responsive data, such as refreshContent = ref(0); refreshContent.value += 1
;
9