为什么在使用NUXT3时不使用动态导入

发布于 2025-02-11 22:45:23 字数 1477 浏览 0 评论 0原文

我正在尝试创建一个组件,该组件可以让我通过我要寻找的图标的名称和通过unplugin-icons动态导入该图标的名称,只要导入的路径是动态的,它似乎都无法正常工作。如果我只是键入一个字符串,则可以正常工作。有什么建议吗?我正在使用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);

以下是我收到的错误,

 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.

我试图使用 / * 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);

下面是标准导入

import IconSun from "~icons/fa-solid/sun";

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. If I were to just type a string it works fine. Any suggestions? I am using Nuxt 3 and 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

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";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最偏执的依靠 2025-02-18 22:45:23

vite已经给您答案,只需阅读它:

如果打算将其按原样保留,则可以使用 /* @vite-ignore
*/在import()呼叫中注释以抑制此警告。

Vite无法分析导入模块,因为它不知道sun的值,

如果这是您的意图,则需要拒绝此错误:

const icon = await import( /* @vite-ignore */` ~icons/fa-solid/${sun}`);

Well vite already give you the answer just read it:

If this is intended to be left as-is, you can use the /* @vite-ignore
*/ comment inside the import() call to suppress this warning.

Vite cannot analyze the importing module because it cant know the value of sun

You need to surpress this error if this is intended:

const icon = await import( /* @vite-ignore */` ~icons/fa-solid/${sun}`);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文