简体   繁体   English

循环导入图像

[英]Import images in react in a loop

What I am trying to accomplish is importing each file from the proper directory. 我要完成的工作是从正确的目录导入每个文件。 My backend code saves files to the images directory, and my frontend directory is where I hold my React.js code. 我的后端代码将文件保存到images目录,而我的前端目录是我存放React.js代码的位置。

The structure of my directory can be seen below. 我的目录结构可以在下面看到。

-- Backend
   -- backend code
-- Frontend
   -- frontend code
-- images
   -- user_1
      -- People.jpg
   -- user_2
      ...

As you can see, I am getting the path from the backend. 如您所见,我正在从后端获取路径。 imagePath will look something like this "../images/user_1/People.jpg". imagePath将类似于以下“ ../images/user_1/People.jpg”。 I saw another stackoverflow post store the image in const variable using the require keyword, but I received an error using that method. 我看到另一个stackoverflow post使用require关键字将图像存储在const变量中,但是使用该方法却收到错误。

@observer
export default class SnapCapsule extends React.Component {
  componentDidMount() {
    axios.get(`${this.props.domain}/snapcapsule`)
      .then(res => {
        res.data.map((capsule) => {
          var domainSize = this.props.domain.length+13;
          var imagePath = "../" + capsule["image"].substring(domainSize);
          var dateToPost = capsule["dateToPost"].substring(0, 10);
          var username = capsule["username"];
          console.log(capsule["caption"], imagePath, dateToPost, username);
          this.props.store.addCapsule(
            capsule["caption"],
            imagePath,
            dateToPost,
            username
          )
        })
      });
  }

  componentWillUnmount() {
    this.props.store.clear();
  }


  render() {
    const { capsules } = this.props.store;
    const capsuleList = capsules.map((capsule, i) => (
    <Col key={i} xs={6} md={4}>
      <Thumbnail src={capsule.imagePath} alt="242x200">
        <h3> {capsule.title} </h3>
        <p> @{capsule.username} </p>
      </Thumbnail>
    </Col>
  ));

return (
  <Grid>
    <Row>
      { capsuleList }

    </Row>
  </Grid>
);

} }

    return (
      <Grid>
        <Row>
          { capsuleList }

        </Row>
      </Grid>
    );
  }
}

Answer 回答

Instead of providing a path to the image tag in the directory. 而不是在目录中提供image标签的路径。 I configured the backen to be retrieved photos through urls. 我将backen配置为通过url检索照片。

capsule.imagePath => http://localhost:8000/snapcapsule/images/user_1/People.jpeg capsule.imagePath => http:// localhost:8000 / snapcapsule / images / user_1 / People.jpeg

I suggest avoid dynamic require for dynamic condition. 我建议避免动态条件的动态需求。 While bundling you it may not include all your image and may cause issue. 捆绑时,它可能不会包括您的所有图像,并且可能会引起问题。

In your condition, you are fetching information from server and trying to import file directly from folder which is not correct way. 在您的情况下,您正在从服务器中获取信息,并尝试直接从文件夹中导入文件,这是不正确的方式。 As web application you should use a web url to show images. 作为Web应用程序,您应该使用Web URL显示图像。

In response of capsuleList, you have image path. 响应capsuleList,您具有图像路径。 Use that path and your backend should handle request and return image. 使用该路径,您的后端应处理请求并返回图像。 It's backend responsibility. 这是后端责任。 While react running on browser, react don't have permission to access folder but your backend can access folders. 当React在浏览器上运行时,React没有访问文件夹的权限,但是您的后端可以访问文件夹。

In your backend, read image and server it. 在您的后端中,读取图像并进行服务器处理。

You should be able to actually reference those images directly in your thumbnail component without importing it as a module or anything like that. 您应该实际上可以直接在缩略图组件中引用这些图像,而无需将其导入为模块或类似的内容。

const path = "./../../images/user_1/People.jpg";
<Thumbnail src={path} ...

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

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