简体   繁体   English

从 Map 连接本地路径 + 动态图像 - React.Js

[英]Concatenate local path + dynamic images from Map - React.Js

i'm trying to do a CardList items from a local data.json file using React.Js.我正在尝试使用 React.Js 从本地data.json文件中执行 CardList 项目。

To load the data i'm using a map function:要加载数据,我正在使用地图功能:

const ItemsList = data.map(item=>{

  return(
  <li><img key={item.id} src={"assets/images/sampleImages/" + item.image}/></li>
  )
})

Code - PasteBin代码 - PasteBin

but i cannot get the image.但我无法获得图像。 No error, just a broken image icon appear.没有错误,只是出现一个损坏的图像图标。

I've try:我试过:

This solution And with URL works, but don't work with path.此解决方案并使用 URL 有效,但不适用于路径。

Also this thread . 还有这个线程 And nothing works.没有任何效果。

破碎的图像来说明。

Firstly import the image like this (You can amend it accordingly)首先像这样导入图像(您可以相应地修改它)

import imageBaseURL = "./assets/images/sampleImages/";

Then inside your ItemList make use of Template Literals like this :然后在你的 ItemList 中使用这样的模板文字

const ItemsList = data.map( item => {
  return(
  <li><img key={item.id} alt="TitleOfImage" src={`${imageBaseURL}${item.image}`}/></li>
  )
})

First step:第一步:

Importing image and store it in one variable ( ExampleImg )导入图像并将其存储在一个变量中( ExampleImg )

import ExampleImg from '../example.png';

Second step:第二步:

Inject the image as a src将图像作为 src 注入

<img src = { ExampleImg } />

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM