Vue.component('CompA', {
template: `<div>CompA</div>`
});
Vue.component('CompB', {
template: `<div>CompB</div>`
});
const vm = new Vue({
el: '#app',
data: {
currentComp: 'CompA'
},
methods: {
changeComp() {
if (this.currentComp === 'CompA') {
this.currentComp = 'CompB';
} else {
this.currentComp = 'CompA';
}
}
}
});
<div id="app">
<!-- 动态组件 -->
<component :is="currentComp"></component>
<button @click="changeComp">切换组件</button>
</div>