react 项目中引入图片的几种方式

Posted by YuHo Wang on 2019-05-27

引用本地图片

  • 导入图片路径
1
2
import Img from './images/1.png'
<img src={Img} alt='' />
  • 直接获取图片
1
<img src={require("./images/1.png")} alt="">

导入背景图

  • css 中用正常 css 语法
1
2
3
.img {
background: url('./images/1.png') 0 0 no-repeat;
}
  • 组件中操作 style 属性
1
style={{background: `url(${require("./images/1.png")})`}}