亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在ReactJs中根據動態路徑加載圖像

在ReactJs中根據動態路徑加載圖像

我正試圖在我正在制作的購物車中顯示圖像,但它沒有顯示出來。我必須導入每個圖像嗎?我知道我的路徑很好,因為它之前有用。我認為我的product.js文件可能有問題,但我無法理解。這是我的Product.jsimport React, { Component, PropTypes } from 'react';class Product extends Component {handleClick = () => {    const { id, addToCart, removeFromCart, isInCart } = this.props;    if (isInCart) {        removeFromCart(id);    } else {        addToCart(id);    }}render() {    const { name, price, currency, image, url, isInCart } = this.props;    return (        <div className="product thumbnail">            <img src={image} alt="product" />            <div className="caption">                <h3>                    <a href={url}>{name}</a>                </h3>                <div className="product__price">{price} {currency}</div>                <div className="product__button-wrap">                    <button                        className={isInCart ? 'btn btn-danger' : 'btn btn-primary'}                        onClick={this.handleClick}>                        {isInCart ? 'Remove' : 'Add to cart'}                    </button>                </div>            </div>        </div>    );}}Product.propTypes = {id: PropTypes.number.isRequired,name: PropTypes.string.isRequired,price: PropTypes.number,currency: PropTypes.string,image: PropTypes.string,url: PropTypes.string,isInCart: PropTypes.bool.isRequired,addToCart: PropTypes.func.isRequired,removeFromCart: PropTypes.func.isRequired,}export default Product;數據來自此產品.jsconst data = [{id: 1,name: 'Reggae Blaster',price: 10,currency: 'GOLD',image: '../assets/blaster_1.png'},{id: 2,name: 'Juicy Blaster',price: 10,currency: 'GOLD',image: 'images/02.jpg'},{id: 4,name: 'Full Body Reggae Armor',price: 20,currency: 'GOLD',image: 'images/04.jpg'},{id: 6,name: 'Reggae Spikes Left',price: 5,currency: 'GOLD',image: 'images/06.jpg'},{id: 5,name: 'Reggae Spikes Right',price: 5,currency: 'GOLD',image: 'images/05.jpg'},{我正在提取所有數據,除了圖像沒有顯示
查看完整描述

3 回答

?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

假設您使用的是webpack,則需要導入圖像才能顯示它


<img src={require('images/06.jpg')} alt="product" />

現在你的圖像數據是動態的,直接指定導入路徑就像


<img src={require(image)} alt="product" />

不起作用。


但是,您可以通過使用模板文字來導入圖像


<img src={require(`${image}`)} alt="product" />

所以你的代碼看起來像


render() {

    const { name, price, currency, image, url, isInCart } = this.props;


    return (

        <div className="product thumbnail">

            <img src={require(`${image}`)} alt="product" />

            <div className="caption">

                <h3>

                    <a href={url}>{name}</a>

                </h3>

                <div className="product__price">{price} {currency}</div>

                <div className="product__button-wrap">

                    <button

                        className={isInCart ? 'btn btn-danger' : 'btn btn-primary'}

                        onClick={this.handleClick}>

                        {isInCart ? 'Remove' : 'Add to cart'}

                    </button>

                </div>

            </div>

        </div>

    );

}

PS還要確保圖像路徑相對于要導入它們的文件。


查看完整回答
反對 回復 2019-09-06
?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

您必須將這些圖像放在捆綁應用程序所在的文件夾中。


查看完整回答
反對 回復 2019-09-06
?
藍山帝景

TA貢獻1843條經驗 獲得超7個贊

嗨Shubham Khatri,這種方法解決了我的問題但我得到了動態添加圖像的下一個警告:警告在./index.html模塊解析失?。阂馔獾牧钆疲?:0)您可能需要一個合適的加載器來處理這種文件類型。| <!DOCTYPE html> | <html lang =“en”>

查看完整回答
反對 回復 2019-09-06
  • 3 回答
  • 0 關注
  • 2579 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號