【问题标题】:backgroundImage carousel: Why wont my react.js array of local images render?背景图像轮播:为什么我的 react.js 本地图像数组无法渲染?
【发布时间】: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="&#9664;" />
                <ImageSlide url={ imgUrls[this.state.currentImageIndex] } />
                <Arrow direction="right" clickFunction={ this.nextSlide } glyph="&#9654;" />
            </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 identifier for 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


【解决方案1】:

如果你使用 webpack,你需要使用require('./imagedirectory')

原来是这样的……

const imgUrls = [ require("./images/croissant.jpg") , require("./images/herbal-tea.jpg") , require("./images/matcha-latte.jpg") , require("./images/mochaLatte.jpg") , require("./images/waffle.jpg") ];

【讨论】:

  • 我进行了更改,但它们仍未呈现。我不知道我正在使用 webpack,我以前从未使用过它,但我可以查看它
  • 我得到相同的未捕获语法错误加上这两个:Failed to load resource: the server responded with a status of 401 (Unauthorized) workspace-html5.js:228 warning: possible EventEmitter memory leak detected. " + eventList.length + " listeners of type "setPath" added. Use emitter.setMaxListeners() to increase limit.
  • 嗯,我认为这是服务器问题。您能否注释掉轮播组件中的所有变量并仅渲染render() {return(&lt;div&gt;carousel page&lt;/div&gt;)} 以测试它不是图像文件的问题。
  • 好的,试过了,现在这是唯一的错误消息Uncaught SyntaxError: Unexpected identifier
  • 它说 carousel.js:1
【解决方案2】:

这个呢:

const ImageSlide = ({ url }) => {

    const img = require(`${url}`);

    const styles = {
        backgroundImage: `url(${img})`,
        backgroundSize: 'cover',
        backgroundPosition: 'center'
    };

    return (
        <div className="image-slide" style={styles}></div>
    );
}

【讨论】:

  • 它正在使用我的“create-react-app”测试。你有任何错误日志吗?
  • 是的,这里有错误Uncaught SyntaxError: Unexpected identifierFailed to load resource: the server responded with a status of 404 (Not Found)Failed to load resource: the server responded with a status of 403 ()Failed to load resource: the server responded with a status of 499 (unknown)
猜你喜欢
  • 2014-11-26
  • 1970-01-01
  • 1970-01-01
  • 2019-09-22
  • 1970-01-01
  • 2021-07-15
  • 2017-04-26
  • 1970-01-01
  • 2020-09-03
相关资源
最近更新 更多