Home > Web Front-end > Vue.js > body text

How to use vue3 dynamic components

PHPz
Release: 2023-05-12 18:49:06
forward
901 people have browsed it

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 },
}
Copy after login

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;
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template