Soalan: Mengapa vue3 perlu menggunakan markRow untuk komponen yang diimport?
<template> <div> <component :is="A"></component> </div> </template> <script> import A from './A'; export default { name: 'Home', data() { return {} }, components: { A }, }
<template> <ul> <li v-for='(item,index) in tabList' :key='index' @click='change(index)' > {{ item.name }} </li> </ul> <keep-alive> <component :is="currentComponent"></component> </keep-alive> </template> <script setup> import A from '../components/A.vue' import B from '../components/B.vue' import C from '../components/C.vue' let tabList = reactive([ {name:'组件A',com:markRaw(A)}, {name:'组件B',com:markRaw(B)}, {name:'组件C',com:markRaw(C)} ]); let currentComponent = reactive({ com:tabList[0] }); const change = ( idx )=>{ currentComponent = tabList[idx].com; }
Atas ialah kandungan terperinci Cara menggunakan komponen dinamik vue3. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!