【发布时间】:2018-08-24 13:36:39
【问题描述】:
我正在向我的网站添加图片轮播。链接到 carousel.js 的 div 是完全空白的。似乎图像没有渲染。我的图像是本地的,并存储在 carousel.js 中的一个数组中。我经历了类似的线程,但无法获得要渲染的图像。例如,我将图像尺寸缩小,尝试在 carousel.js 顶部导入图像,将图像文件夹移动到 node 模块生成的公共文件夹,以及其他大约十件事。这是我的第一个反应项目,所以任何帮助将不胜感激。
完整项目可以在cloud 9上查看here
这是我的 carousel.js:
import React from 'react';
import ReactDOM from 'react-dom';
const imgUrls = [
"./images/croissant.jpg",
"./images/herbal-tea.jpg",
"./images/matcha-latte.jpg",
"./images/mochaLatte.jpg",
"./images/waffle.jpg"
];
class Carousel extends React.Component {
constructor (props) {
super(props);
this.state = {
currentImageIndex: 0
};
this.nextSlide = this.nextSlide.bind(this);
this.previousSlide = this.previousSlide.bind(this);
}
previousSlide () {
const lastIndex = imgUrls.length - 1;
const { currentImageIndex } = this.state;
const shouldResetIndex = currentImageIndex === 0;
const index = shouldResetIndex ? lastIndex : currentImageIndex - 1;
this.setState({
currentImageIndex: index
});
}
nextSlide () {
const lastIndex = imgUrls.length - 1;
const { currentImageIndex } = this.state;
const shouldResetIndex = currentImageIndex === lastIndex;
const index = shouldResetIndex ? 0 : currentImageIndex + 1;
this.setState({
currentImageIndex: index
});
}
render () {
return (
<div className="carousel">
<Arrow direction="left" clickFunction={ this.previousSlide } glyph="◀" />
<ImageSlide url={ imgUrls[this.state.currentImageIndex] } />
<Arrow direction="right" clickFunction={ this.nextSlide } glyph="▶" />
</div>
);
}
}
const Arrow = ({ direction, clickFunction, glyph }) => (
<div
className={ `slide-arrow ${direction}` }
onClick={ clickFunction }>
{ glyph }
</div>
);
const ImageSlide = ({ url }) => {
const styles = {
backgroundImage: `url(${url})`,
backgroundSize: 'cover',
backgroundPosition: 'center'
};
return (
<div className="image-slide" style={styles}></div>
);
}
ReactDOM.render(
<Carousel />,
document.getElementById('container')
);
【问题讨论】:
-
您有任何错误吗?我认为这是一个 webpack 配置问题。
-
Uncaught SyntaxError: Unexpected identifierfor carousel.js -
Failed to load resource: the server responded with a status of 428 (Precondition Required) -
Error: EDISCONNECT: client connection went away at module.exports.ReliableSocket.disconnect (workspace-html5.js:43980) at workspace-html5.js:43964 at wrapped (workspace-html5.js:7447) -
在你的图像数组中我认为你需要
require("./images/croissant.jpg")
标签: javascript reactjs image carousel