Rua*_*uan 4 file require node.js
奇怪的问题
我动态地将图像的路径包含在源目录中。
将图像目录作为字符串放入工作正常(我注释掉的部分),但是一旦我将其放入变量中,它就会给我错误“找不到模块”。””
var imageDir="assets/img/MyImage.png";
--Working // const imageData= require('assets/img/MyImage.png');
--Not Working const imageData= require(imageDir);
Run Code Online (Sandbox Code Playgroud)
有人知道为什么吗?
不幸的是这里 没有答案
Webpack 需要知道在编译时要捆绑哪些文件,但表达式(变量)的真实路径值只能在运行时给出,您需要require.context:
/* If the structure is like:
src -
|
-- index.js (where these codes are deployed)
|
-- assets -
|
--img
*/
let assetsPath = require.context('./assets/img', false, /\.(png|jpe?g|svg)$/);
// See where are the images after bundling.
// console.log(assetsPath('./MyImage.png'));
// You can put all the images you want in './assets/img', and access it.
var newElement = {
"id": doc.id,
"background": assetsPath('./MyImage.png');
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2038 次 |
| 最近记录: |