[英]Why can I not use a dynamic import when using Nuxt3
I am trying to create a component that will let me pass in the name of the icon I am looking for and import that icon dynamically via unplugin-icons, it seems to be not working whenever the path for the import is dynamic.我正在尝试创建一个组件,让我传入我正在寻找的图标的名称并通过 unplugin-icons 动态导入该图标,只要导入的路径是动态的,它似乎就不起作用。 If I were to just type a string it works fine.
如果我只是输入一个字符串,它工作正常。 Any suggestions?
有什么建议么? I am using Nuxt 3 and Vite.
我正在使用 Nuxt 3 和 Vite。
interface Icon {
name: string;
}
const props = defineProps<Icon>();
const sun = "sun";
const icon = await import(`~icons/fa-solid/${sun}`);
const PropIcon = defineComponent(icon.default);
Below is the error I recieve以下是我收到的错误
const props = __props;
12 | const sun = "sun";
13 | const icon = ([__temp, __restore] = _withAsyncContext(() => import(`~icons/fa-solid/${sun}`)), __temp = await __temp, __restore(), __temp);
| ^
14 | const PropIcon = defineComponent(icon.default);
15 | const __returned__ = { props, sun, icon, PropIcon };
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.
I tried to use the /* vite ignore */ but it did not work我尝试使用 /* vite ignore */ 但它不起作用
Below is the hardcoded version which works以下是有效的硬编码版本
interface Icon {
name: string;
}
const props = defineProps<Icon>();
const sun = "sun";
const icon = await import(`~icons/fa-solid/sun`);
const PropIcon = defineComponent(icon.default);
Below is the standard import下面是标准导入
import IconSun from "~icons/fa-solid/sun";
Well vite already give you the answer just read it:好吧,vite 已经给你答案了,请阅读它:
If this is intended to be left as-is, you can use the /* @vite-ignore */ comment inside the import() call to suppress this warning.
如果打算保持原样,您可以在 import() 调用中使用 /* @vite-ignore */ 注释来抑制此警告。
Vite cannot analyze the importing module because it cant know the value of sun
Vite 无法分析导入模块,因为它无法知道
sun
的值
You need to surpress this error if this is intended:如果这是有意的,您需要抑制此错误:
const icon = await import( /* @vite-ignore */` ~icons/fa-solid/${sun}`);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.