<template>
<component :is="currentComponent" />
</template>
<script>
export default {
data() {
return {
currentComponent: 'ComponentA', // 初始组件为 ComponentA
};
},
methods: {
changeComponent(newComponent) {
this.currentComponent = newComponent;
}
},
components: {
ComponentA: {
template: '<div>This is Component A</div>'
},
ComponentB: {
template: '<div>This is Component B</div>'
}
}
};
</script>
<component :is="currentComponent">:
:is 绑定一个变量 currentComponent 来决定渲染哪个组件。data():
data 中定义了一个 currentComponent 变量,默认值为 'ComponentA',表示初始渲染的组件是 ComponentA。methods:
changeComponent 方法,用于更改 currentComponent 的值,从而切换显示的组件。components:
ComponentA 和 ComponentB,每个组件都有一个简单的模板。通过这种方式,你可以根据需要动态地创建和切换不同的组件。
上一篇:npm启动vue项目
下一篇:vue3双向绑定
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站