代码之家  ›  专栏  ›  技术社区  ›  Sajjad Tabreez

绝对url不能放在react中的点运算符之前

  •  0
  • Sajjad Tabreez  · 技术社区  · 7 年前

    我在react中创建了一个应用程序。但是图像没有渲染。

    下面是我在代码中实现的。

    import React, { Component } from 'react';
    import { Link } from 'react-router-dom';
    
    
    import axios from 'axios';
    
    class PLPMenu extends Component {
    
      state = {
        shoeCategory: []
      }
    
    
    
      componentDidMount() {
        const url = 'GirlShoeCategory'
    
    
    
        axios.get(`http://localhost:3030/${url}`)
          .then(res => {
            console.log(res.data.express.catalogEntryView);
            this.setState({
              shoeCategory: res.data.express.catalogEntryView
            })
          })
      }
    
    
    
      render() {
        const { shoeCategory } = this.state;
        const picUrl = 'https://149.129.128.3:8443'
    
        return (
    
          <div>
            <div className="container">
              <div className="row">
                {
                  shoeCategory.map(shoeList => (
    
                    <div className="col-md-4">
    
                      <h2 key={shoeList.uniqueID}></h2>
    
    
                      <img src={shoeList + picUrl + .thumbnail}/>
                      <Link to="/PDP"><p className="pdp">{shoeList.name}</p></Link>
                      <p>{shoeList.price[0].value}</p>
    
    
                    </div>
    
                  ))
                }
              </div>
            </div>
    
          </div>
    
    
    
    
        )
    
      }
    
    }
    
    export default PLPMenu;  
    

    在上面的代码中,在map函数下,我实现了{shoeList.thumbnail}。但是,图像没有渲染。它需要在缩略图之前添加我无法实现的绝对路径。有人能帮我把它连起来吗。

    2 回复  |  直到 7 年前
        1
  •  1
  •   simon    7 年前

    假设图像的完整路径是:

    https://149.129.128.3:8443/wcsstore/ExtendedSitesCatalogAssetStore/images/catalog/apparel/girls/gsh020_shoes/200x310/gsh020_2001.jpg

    假设 shoeList

    [object Object]https://149.129.128.3:8443/wcsstore/ExtendedSitesCatalogAssetStore/images/catalog/apparel/girls/gsh020_shoes/200x310/gsh020_2001.jpg

    根据您的描述,以下修复可能就是您所做的:

    <img src={picUrl + shoeList.thumbnail} />
    

    https://149.129.128.3:8443/wcsstore/扩展站点scatalogassetstore/images/catalog/apparel/girls/gsh020\u shoes/200x310/gsh020\u 2001.jpg

        2
  •  1
  •   V-rund Puro-hit    7 年前

    我不完全确定我是否正确理解你,但你可以使用模板字符串串联。

    const imageSrc = `${picUrl}${shoeList}${shoeList.thumbnail}`
    

    希望这能帮到你。