1.不使用setup语法糖,这种方式和vue2差不多,is可以是个字符串
<template>
<Child1 />
<Child2 />
<component :is="currentComp"></component>
<el-button @click="compChange">切换组件</el-button>
</template>
<script>
import { ref } from 'vue'
import Child1 from './Child1.vue'
import Child2 from './Child2.vue'
export default {
components: {
Child1,
Child2
},
setup() {
let currentComp = ref('Child1')
// 切换组件
const compChange = () => {
if(currentComp.value == 'Child1') {
currentComp.value = 'Child2'
}else {
currentComp.value = 'Child1'
}
}
return {
currentComp,
compChange