【发布时间】:2016-08-02 19:02:59
【问题描述】:
我有一个用 React 和 Django 构建的项目。我也在使用 Webpack 来加载更改。我想显示本地驱动器中的图像。
我要显示照片的文件位于:
myProject/brandplug/static/app/photoGrid/SampleDisplay.jsx
照片位于:
myProject/brandplug/static/location_photos/
index.jsx 文件位于:
myProject/brandplug/static/app/index.jsx
渲染 React 的 html 文件位于:
myProject/accounts/templates/index.html
所以我从SampleDisplay.jsx 尝试导入这样的图像:
const itemStyle = {
backgroundImage: `{require("../location_photos/" + ${photoName})}`
};
然后:
<div style={itemStyle}>
</div>
我也尝试使用"../../location_photos/" 和"../../brandplug/static/location_photos/",而不是"../location_photos/"。
但是,图像仍未加载。
如何加载它们?
【问题讨论】:
-
使用前需要先获取图片。在
webpack你应该需要文件然后使用它,因为我看到你正在尝试通过名称动态地需要文件。你需要做这样的事情jsfiddle.net/6uLLus9v/1 -
但是我需要它,如上图。
-
你不能像你那样做。 webpack 在构建过程之前需要 require 文件,并且不能动态加载文件
标签: javascript django reactjs webpack