Vue3 中使用 Root Div 包裹 Keep-Alive 组件
在 Vue 3 中,可以使用 `
首先,在需要使用 `
<teleport to='#root'>
<keep-alive>
<!-- 需要缓存的组件内容 -->
</keep-alive>
</teleport>
其中,'#root' 是指定的目标父级元素的选择器。
然后,在根组件中,将 `
<template>
<div id='root'>
<router-view />
<teleportTarget />
</div>
</template>
<script>
import { Teleport } from 'vue'
export default {
components: {
TeleportTarget: Teleport,
},
}
</script>
在上面的示例中,使用了 'id' 属性来指定 `
这样,就可以在 Vue 3 中使用根级别的 `
` 元素来包裹 `` 组件了。
原文地址: https://www.cveoy.top/t/topic/lNpN 著作权归作者所有。请勿转载和采集!