组件的动态导入不适用于变量的组件路径
堆栈
- VUE 3.2
- NUXT 3.0.0-RC.4
- VITE 2.9
试图
通过变量而不是静态字符串的&lt;组件动态加载组件:IS /&gt; < /code>。
代码
这是我要实现的范围的示例:
<script lang="ts" setup>
import { defineAsyncComponent } from 'vue'
const filepathStatic = '../foobar/Foobar.vue'
const bar = 'bar'
const filepathDynamic = `../foobar/Foo${bar}.vue` // meets all requirements
const asyncComponent = defineAsyncComponent(() => {
return import('../foobar/Foobar.vue') // WORKS
return import(filepathStatic) // does NOT work
return import(filepathDynamic) // does NOT work
})
</script>
<template>
<div>
<component :is="asyncComponent" />
</div>
</template>
错误
1。)vite-error
The above dynamic import cannot be analyzed by vite.
See https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations for supported dynamic import formats. If this is intended to be left as-is, you can use the /* @vite-ignore */ comment inside the import() call to suppress this warning.
=&gt;动态文件名满足此页面上所述的所有要求。
2。)nuxt-error
500
Cannot read properties of undefined (reading 'stubModule')
at __instantiateModule__ (./.nuxt/dist/server/server.mjs:4094:11)
at __ssrLoadModule__ (./.nuxt/dist/server/server.mjs:4085:25)
at ssrImport (./.nuxt/dist/server/server.mjs:4110:13)
at ssrDynamicImport (./.nuxt/dist/server/server.mjs:4121:12)
at ./.nuxt/dist/server/server.mjs:2808:14
at load (/node_modules/.pnpm/@[email protected]/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:2255:17)
at setup (/node_modules/.pnpm/@[email protected]/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:2308:24)
at callWithErrorHandling (/node_modules/.pnpm/@[email protected]/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:157:22)
at setupStatefulComponent (/node_modules/.pnpm/@[email protected]/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7084:29)
at setupComponent (/node_modules/.pnpm/@[email protected]/node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:7039:11)
=&gt;我不知道,为什么这不起作用。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是大多数工具进行动态导入的方式(无论是WebPack,Vite,lollup)
请参阅这个部分滚动的 dynamic-import-vars
这意味着:
This is how most tools do dynamic imports (be it Webpack, Vite, Rollup)
See this part of the docs of Rollup's dynamic-import-vars plugin used in the Vite
This means:
方案
我在NUXT文档中找到了解决方案的解决 : https://v3.nuxtjs.org/guide/directory-scrupture/components/#dynamic-components
1.)本地(建议)
2.)全球(不建议)
The solution
I found the solution in the Nuxt documentation: https://v3.nuxtjs.org/guide/directory-structure/components/#dynamic-components
1.) locally (recommended)
2.) globally (not recommended)