未找到模块:无法解析“../../assets/img-3.jpg”中的“../../assets/img-3.jpg”

adi*_*mar 11 reactjs reactstrap

我正在尝试将本地图像导入 reactjs。但它不起作用。我正在使用 reactstrap 制作旋转木马。

这是错误

找不到模块:无法解析“C:\Users\adity\Desktop\foodcubo-dev\project\src\Layouts\header”中的“../../assets/img-3.jpg”

我尝试使用导入方法导入图像,尽管图像在资产中可用。我想进口没问题。问题可能与渲染有关。我不知道。

这是我的代码。

头文件.js

import React, { Component } from 'react';
import imgone from '../../assets/img-1.jpg'
import imgtwo from '../../assets/img-2.jpg'
import imgthree from '../../assets/img-3.jpg'

import {
  Carousel,
  CarouselItem,
  CarouselIndicators,
  CarouselCaption
} from 'reactstrap';

const items = [
    {
    src: {imgone},
    altText: 'Slide 1',
    caption: 'Slide 1'
  },
    {
    src: {imgtwo},  
    altText: 'Slide 2',
    caption: 'Slide 2'
  },
    {
    src: {imgthree},
    altText: 'Slide 3',
    caption: 'Slide 3'
  }
];

class Header extends Component {
  constructor(props) {
    super(props);
    this.state = { activeIndex: 0 };
    this.goToIndex = this.goToIndex.bind(this);
    this.onExiting = this.onExiting.bind(this);
    this.onExited = this.onExited.bind(this);
  }

  onExiting() {
    this.animating = true;
  }

  onExited() {
    this.animating = false;
  }


  goToIndex(newIndex) {
    if (this.animating) return;
    this.setState({ activeIndex: newIndex });
  }

  render() {
    const { activeIndex } = this.state;

    const slides = items.map((item) => {
      return (
        <CarouselItem
          onExiting={this.onExiting}
          onExited={this.onExited}
          key={item.src}
        >
          <img src={item.src} alt={item.altText} />
          <CarouselCaption captionText={item.caption} captionHeader={item.caption} />
        </CarouselItem>
      );
    });

    return (
      <Carousel
        activeIndex={activeIndex}
        next={this.next}
        previous={this.previous}
      >
        <CarouselIndicators items={items} activeIndex={activeIndex} onClickHandler={this.goToIndex} />
        {slides}
      </Carousel>
    );
  }
}


export default Header;
Run Code Online (Sandbox Code Playgroud)

小智 5

我确实遇到了同样的问题,这样做解决了我的问题:

./../../assets/img-3.jpg
Run Code Online (Sandbox Code Playgroud)