要求(img路径)不工作/找不到模块“。” reactjs

时间:2017-08-17 13:56:49

标签: javascript reactjs

您好我正在尝试使用react.js将this.state中的一组图像映射到图像标记。我遇到了错误:“找不到模块'。'”

这是错误:     错误:找不到模块“。”     webpackMissingModule     SRC /组件/ thumbnails.js:26

 23 | }
  24 | render(){
  25 |  const thumbnailimg = this.state.thumbnail_vids.map((img,i)=>{
> 26 |      let image = require(img);
  27 |      return(<img key={i}  className="thumbimg" src={image}/>)
  28 |     })
  29 |  return(

以下是完整代码:thumbnails.js

import React, { Component } from "react";
import { render } from "react-dom";

class Thumbnails extends Component {
    constructor(props){
        super(props);
        this.state = {
            thumbnail_vids: ['../thumbnails/asthma_1.jpeg','../thumbnails/asthma_2.jpeg','../thumbnails/asthma_3.jpeg']
        }
    }
    render(){
        const thumbnailimg = this.state.thumbnail_vids.map((img,i)=>{
            let image = require(img);
            return(<img key={i}  className="thumbimg" src={image}/>)
        })
        return(
            <div>
                <span className="thumbtable">
                <img src={require("../thumbnails/asthma_1.jpeg")} />
                    {thumbnailimg}
                </span>
            </div>
        )
    }
}

export default Thumbnails;

这是我的src文件夹的项目结构(虽然我已抽象出与手头问题无关的任何内容):

        ├── App.css
        ├── App.js
        ├── App.test.js
        ├── components
        │   ├── header.js
        │   ├── likebutton.js
        │   ├── thumbnails.js
        │   └── topicselect.js
        ├── index.css
        ├── index.js
        ├── registerServiceWorker.js
        ├── thumbnails
        │   ├── asthma_1.jpeg
        │   ├── asthma_2.jpeg
        │   ├── asthma_3.jpeg
        │   ├── copd_1.jpeg
        │   ├── copd_2.jpeg
        │   ├── copd_3.jpeg
        │   ├── diabetes_1.jpeg
        │   ├── diabetes_2.jpeg
        │   ├── diabetes_3.jpeg
        │   ├── emphysema_1.jpeg
        │   ├── emphysema_2.jpeg
        │   ├── emphysema_3.jpeg
        │   ├── hyperlipidemia_1.jpeg
        │   ├── hyperlipidemia_2.jpeg
        │   ├── hyperlipidemia_3.jpeg
        │   ├── hypertension_1.jpeg
        │   ├── hypertension_2.jpeg
        │   ├── hypertension_3.jpeg
        │   ├── narcolepsy_1.jpeg
        │   ├── narcolepsy_2.jpeg
        │   ├── narcolepsy_3.jpeg
        │   ├── pneumonia_1.jpeg
        │   ├── pneumonia_2.jpeg
        │   └── pneumonia_3.jpeg

我正在使用create-react-app

3 个答案:

答案 0 :(得分:1)

想出来,解决方案: 需要this.state中的图像,而不是map函数

import React, { Component } from "react";
import { render } from "react-dom";

class Thumbnails extends Component {
    constructor(props){
        super(props);
        this.state = {
            current: 'asthma',
            thumbnail_vids: [require('../thumbnails/asthma_1.jpeg'),require('../thumbnails/asthma_2.jpeg'),require('../thumbnails/asthma_3.jpeg')]
        }
    }
    componentWillReceiveProps(nextProps){
        let current = nextProps.current.topic;
        let thumbnail_vids = [];
        for(let i = 1; i <= 3; i++){
            thumbnail_vids.push(require("../thumbnails/"+current.toLowerCase()+"_"+i+".jpeg"));
            console.log(current);
        }
        this.setState({current,thumbnail_vids,})
    }
    chooseVideo(){

    }
    render(){
        const thumbnailimg = this.state.thumbnail_vids.map((img,i)=>{
            return(<img className="thumbimg" src={img}/>)
        })
        return(
        <div className="vid-and-thumb-holder">
            <div>
                <span className="thumbtable">
                {thumbnailimg}

                </span>
            </div>
        </div>
        )
    }
}

export default Thumbnails;

答案 1 :(得分:0)

你得到的事实

  

错误:找不到模块“。” webpackMissingModule

表示您正在映射第一个字符串,而地图中的第一个项目是字符串中的第一个字符:'。'。

答案 2 :(得分:0)

尝试将 require 语句移动到您的状态,如下所示:

Proof of Concept