简体   繁体   English

我可以使用require('img.jpg')在background-image样式属性内在本地服务器上加载img文件吗?

[英]Can I use require('img.jpg') to load img file on local server within background-image style property?

Can I use require('img.jpg') to load img file on local server within background-image style property? 我可以使用require('img.jpg')在background-image样式属性内在本地服务器上加载img文件吗?

To load img files in a react app on a local server I use <img src= {require('./greenLine.jpg')} /> after installing url-loader and file-loader . 要在本地服务器上的<img src= {require('./greenLine.jpg')}应用程序中加载img文件,请在安装url-loaderfile-loader之后使用<img src= {require('./greenLine.jpg')} /> />。

This works great, but I want to use the img file in a style object, like - const divBkground = { backgroundImage: require('url(./greenLine.jpg)') } 这很好用,但是我想在样式对象中使用img文件,例如const divBkground = { backgroundImage: require('url(./greenLine.jpg)') }

This doesn't work. 这行不通。 Can someone advise? 有人可以建议吗?

THANKS.. 谢谢..

You need not use require when you try to define a style for background image . 尝试为背景图像定义样式时,无需使用require Just the simple backgroundImage: 'url(./greenLine.jpg)' works fine 只是简单的backgroundImage: 'url(./greenLine.jpg)'可以正常工作

const divBkground = { 
    backgroundImage: 'url(./greenLine.jpg)'
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 style =“ background-image不起作用,但是img正在起作用 - style="background-image not working but img is working 将http://i.example.com/img.jpg更改为http://www.example.com/show/i/img.jpg - Change http://i.example.com/img.jpg to http://www.example.com/show/i/img.jpg 如何获取div的背景图像值并将其应用于img标签onclick的src值? - How can I get the background-image value of a div and apply it to the src value of an img tag onclick? 错误:在 react.js 上找不到模块“./img.jpg” - Error: Cannot find module './img.jpg' on react.js 无论在何处使用,如何触发图像重新加载(CSS背景图像,伪元素,样式属性,img标签) - How can I trigger images reload regardless of where they're used (CSS background-image, pseudo-element, style attribute, img tag) 我该如何使用<img>在 Android 上使用本地图像? - How can I use <img> with a local image on Android? 使用 require 无法在反应中加载本地 img - Can't load local img in react using require 解析 img src 和 background-image url 时使用字符串路径而不是要求 - Use string paths to images instead of requires when resolving img src and background-image urls 如何使用 Javascript 而不是使用背景属性<img>标签? - How can I use the background property using Javascript instead of using <img> tag? 我该如何编写一个JS函数来在 <li> 类,并将该图像作为li的CSS中的背景图像值? - How do I write a JS function that finds an img inside a <li> class and makes that image as a background-image value in the li's css?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM