【发布时间】:2021-03-04 12:46:07
【问题描述】:
在 Vue 应用程序中工作 我正在尝试从 node_modules 中的几个依赖项动态导入一个 css 文件。
使用带有静态字符串的require('path') 可以正常工作并加载我的 css 文件。
一旦我尝试将函数参数传递给 require() 函数,它就不起作用了。
const getFontDeclaration = (path) => {
{
return new Promise((resolve) => {
// a static string works fine
require('@foo/own-package-name/dist/assets/css/font-face.css');
// using an argument does not work
require(path);
// import(path)
// does not work either
// while this work fine too
import('@foo/own-package-name/dist/assets/css/font-face.css')
.then(() => {
resolve();
})
});
}
};
有什么线索或方向吗?使用 import() 和 require() 有点混淆
【问题讨论】:
标签: javascript vue.js webpack