<template>
<!--Vue3使用is属性来切换组件-->
<!--KeepAlive标签使组件间切换时被切换的表单值不丢失-->
<KeepAlive>
<component :is="currentTab"></component>
</KeepAlive>
<button @click="updateis">点击切换控件</button>
</template>
<script setup>
import classtest from "./components/classtest.vue";
import classtest2 from "./components/classtest2.vue";
import { ref, shallowRef } from "vue";
//因为切换组件有性能开销问题,所以使用shallowRef或markRaw
//不是用ref
let currentTab = shallowRef(classtest);
function updateis() {
if (currentTab.value === classtest2) {
console.log("classtest2");
currentTab.value = classtest;
} else {
console.log("classtest");
currentTab.value = classtest2;
}
}
</script>
<style scoped>
</style>
Vue动态组件切换is
最新推荐文章于 2024-09-02 10:07:02 发布