Home  >  Article  >  Web Front-end  >  How to use vue3 dynamic components

How to use vue3 dynamic components

PHPz
PHPzforward
2023-05-12 18:49:06834browse

Question: Why does vue3 need to use markRow for introduced components?

vue2

<template>
<div>
<component :is="A"></component>
</div>
</template>

<script>
import A from &#39;./A&#39;;
export default {
name: &#39;Home&#39;,
data() {
return {}
},
components: { A },
}

vue3

<template>
<ul>
<li
v-for=&#39;(item,index) in tabList&#39;
:key=&#39;index&#39;
@click=&#39;change(index)&#39;
>
{{ item.name }}
</li>
</ul>
<keep-alive>
<component :is="currentComponent"></component>
</keep-alive>
</template>

<script setup>
import A from &#39;../components/A.vue&#39;
import B from &#39;../components/B.vue&#39;
import C from &#39;../components/C.vue&#39;
let tabList = reactive([
{name:&#39;组件A&#39;,com:markRaw(A)},
{name:&#39;组件B&#39;,com:markRaw(B)},
{name:&#39;组件C&#39;,com:markRaw(C)}
]);
let currentComponent = reactive({
com:tabList[0]
});
const change = ( idx )=>{
currentComponent = tabList[idx].com;
}

The above is the detailed content of How to use vue3 dynamic components. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete