方法2:使用importimport x1 from ../img"/>

react中如何引入图片?

react中如何引入图片?下面本篇文章给大家介绍一些在react中引入图片的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

react中如何引入图片?

方法1:使用require

<img src={require('../img/iconxx.webp')} alt="" />

方法2:使用import

import x1 from '../img/x1.webp'
import x2 from '../img/x2.webp'
<img src={x1} alt="" />
<img src={x2} alt="" />

方法3:

<img src='http://localhost:8000/react-project/src/img/iconxx.webp' alt="" />  //不推荐使用

背景图片引用方法如下:

const divStyle = {
  color: 'blue',
  backgroundImage: 'url(' + imgUrl + ')',
  // 或者 background: `url${require("xxx.webp")}`
};
function XXXXX Component() {
  return <div style={divStyle}>Iverson.tian!</div>;
}

CSS/Less中设置背景色:

background: url(../static/login-bg.webp) no-repeat center center fixed;
background-size: cover;

更多web前端开发知识,请查阅 HTML中文网 !!

以上就是react中如何引入图片?的详细内容,更多请关注易知道|edz.cc其它相关文章!

推荐阅读