我正在尝试将本地图像导入reactjs。但是它不起作用。我正在使用reactstrap制作轮播。
这是错误
未找到模块:无法解析'C:\ Users \ adity \ Desktop \ foodcubo-dev \ project \ src \ Layouts \ header'中的'../../ assets / img-3.jpg'< / p>
我尝试使用导入方法导入图像,但是资产中有可用的图像。我想导入很好。问题可能与渲染有关。我不知道。
这是我的代码。
Header.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;
答案 0 :(得分:3)
我确实遇到了同样的问题,并且这样做解决了我的问题:
./../../assets/img-3.jpg
答案 1 :(得分:0)
不确定,但可以尝试:
import imgone from './assets/img-1.jpg'
import imgtwo from './assets/img-2.jpg'
import imgthree from './assets/img-3.jpg'
或者如果不起作用,请尝试:
const imgone = require('../../assets/img-1.jpg');
const imgtwo = require('../../assets/img-2.jpg');
const imgthree = require('../../assets/img-3.jpg');
如果仍然无法正常运行,请查看https://stackoverflow.com/a/36699941/3625433