Reactjs-如何使用Reactjs照片库显示导入的图像



我一直在玩react照片库。

我把它和灯箱一起安装了,当我显示在线照片时,它运行得很好。

当我尝试显示导入的,它不是显示照片,只是它的框架

import React from 'react';
import Gallery from 'react-photo-gallery';
import Lightbox from 'react-images';
import thumbsup from "../../assets/images/thumbsup.png";

const photos = [
{ src: {thumbsup}, width: 4, height: 3 },
{ src: 'https://source.unsplash.com/Dm-qxdynoEc/800x799', width: 1, height: 1 },
{ src: 'https://source.unsplash.com/qDkso9nvCg0/600x799', width: 3, height: 4 }
];

export default class Sample extends React.Component {
constructor() {
super();
this.state = { currentImage: 0 };
this.closeLightbox = this.closeLightbox.bind(this);
this.openLightbox = this.openLightbox.bind(this);
this.gotoNext = this.gotoNext.bind(this);
this.gotoPrevious = this.gotoPrevious.bind(this);
}
openLightbox(event, obj) {
this.setState({
currentImage: obj.index,
lightboxIsOpen: true,
});
}
closeLightbox() {
this.setState({
currentImage: 0,
lightboxIsOpen: false,
});
}
gotoPrevious() {
this.setState({
currentImage: this.state.currentImage - 1,
});
}
gotoNext() {
this.setState({
currentImage: this.state.currentImage + 1,
});
}
render() {
return (
<div>
<Gallery photos={photos} onClick={this.openLightbox} />
<Lightbox images={photos}
onClose={this.closeLightbox}
onClickPrev={this.gotoPrevious}
onClickNext={this.gotoNext}
currentImage={this.state.currentImage}
isOpen={this.state.lightboxIsOpen}
/>
</div>
)
}
}

我正在尝试像在其他组件中那样显示它。

import thumbsup from "../../assets/images/thumbsup.png";
{ src: {thumbsup}, width: 4, height: 3 }

去掉{thumbsub}周围的大括号应该可以解决您的问题。

const photos = [
{ src: thumbsup, width: 4, height: 3 },
{ src: 'https://source.unsplash.com/Dm-qxdynoEc/800x799', width: 1, height: 1 },
{ src: 'https://source.unsplash.com/qDkso9nvCg0/600x799', width: 3, height: 4 }
];

就像你现在拥有的一样,照片对象看起来像这样:

{
src: {
thumbsup: "/..../....png"
}, 
width: 4, 
height: 3 
}

最新更新