学习如何在Vue 3中实现动态组件导入
P粉253800312
P粉253800312 2023-08-24 16:00:23
[Vue.js讨论组]
<p>根据这篇文章,我想要将组件动态地导入到我的Vue 3应用程序中。视图的代码如下:</p> <pre class="brush:php;toolbar:false;">&lt;template&gt; &lt;div class="page"&gt; &lt;latest-box v-if="showLatestBox" /&gt; &lt;/div&gt; &lt;/template&gt; &lt;script&gt; // @ 是 /src 的别名 // 这种方式有效 //import LatestBox from '@/components/LatestBox.vue' export default { name: 'Page 1', data() { return { showLatestBox: true, } }, components: { LatestBox: () =&gt; import('@/components/LatestBox.vue') // 这种方式无效 } } &lt;/script&gt;</pre> <p>代码没有报错,但是我在页面上看不到组件。如果我使用第一种导入方式,它可以工作。我漏掉了什么吗?</p>
P粉253800312
P粉253800312

全部回复(1)
P粉970736384

在Vue 3中,你需要使用defineAsyncComponent来懒加载组件

import { defineAsyncComponent } from 'vue'
...
    components: {
        LatestBox: defineAsyncComponent(() => import('@/components/LatestBox.vue'))
    }

https://v3-migration.vuejs.org/breaking-changes/async-components.html#overview

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号