webpack - 如何从 JS 中引用这个外部图像资源?
问题描述
我有一个使用 webpack 和 sass 的 React 应用程序。我远不是这些技术的专家。该应用程序index.scss
在顶级 html 元素上使用背景图像,如下所示:
html {
...
background-image: url('./path/to/image/file.jpg');
...
}
使用浏览器的开发工具,我可以看到 webpack 已经移动并重命名了图像文件:
background-image: url(http://localhost:6006/c165f4b41cec0.jpg)
现在我想在一些使用React 内联样式的 JS 中引用相同的图像资源:
const SomeComponent = ()=> (
<div
style={{
...
backgroundImage: ??? what goes here ???
...
}}
>
Some text here...
</div>
);
无论 webpack 用它做什么,我怎样才能从 JS 中可靠地引用相同的图像资源?
我可以将内联样式转移到另一个 scss 文件中,但我不想这样做,因为我想保持这个特定的 JS 文件自包含。
谢谢。
解决方案
我解决了,所以我只是在回答我自己的问题……哎呀。
import jpgFile from './path/to/image/file.jpg'; // <-- add this import
const SomeComponent = ()=> (
<div
style={{
...
backgroundImage: 'url(' + jpgFile + ')', // or `url(${jpgFile})`
...
}}
>
Some text here...
</div>
);
推荐阅读
- c - 执行for循环
- python - Jinja2 Django Python groupby('attribute') 自定义排序的石斑鱼
- python - 如何将 django 模型字段传递给函数并将变量返回到上下文以在模板中使用
- javascript - 该参数应作为 javascript 正文中的函数执行
- python - 如何在烧瓶中使用 Ajax 提供的数据?
- linux - 如何从 shell 中的三台机器中随机选择第一台机器?
- yii2 - 如何获取今天在 yii2 中创建的项目的数量?
- django - Django ajax重定向在同一个div中打开
- python - 散景python中的分类条形图
- mongodb - Mongodb $in 查询其他条件