Vue 3 中使用 keep-alive 组件缓存动态组件 - 示例讲解
在 Vue 3 中,可以使用`
<template>
<div>
<button @click="toggleComponent">Toggle Component</button>
<keep-alive>
<component :is="currentComponent"></component>
</keep-alive>
</div>
</template>
<script>
import ComponentA from './ComponentA.vue'
import ComponentB from './ComponentB.vue'
export default {
data() {
return {
currentComponent: 'ComponentA'
}
},
methods: {
toggleComponent() {
this.currentComponent = this.currentComponent === 'ComponentA' ? 'ComponentB' : 'ComponentA'
}
},
components: {
ComponentA,
ComponentB
}
}
</script>
在这个例子中,我们有两个组件`ComponentA`和`ComponentB`。通过点击'Toggle Component'按钮,我们可以切换当前显示的组件。使用`
注意,`
原文地址: https://www.cveoy.top/t/topic/qiOf 著作权归作者所有。请勿转载和采集!