简体   繁体   English

如何在ReactJS的html标签中通过axios请求获取api中的图像?

[英]How use image in api get by axios request in html tags in ReactJS?

I use axios to get data from the api, now I need to display the image in the td tag in HTML, the text response are working fine but the image is not displaying.我使用 axios 从 api 获取数据,现在我需要在 HTML 的td标签中显示图像,文本响应工作正常但图像未显示。

my request code is:我的请求代码是:

componentDidMount(){

      var headers = {
        "Content-Type":"application/json",
        "AccessToken":localStorage.TOKEN,
      }

      axios.get(window.$_APIurl+"myProviders/5e463d53e7179a2f011c4104", {headers: headers})
        .then((response) => {   
          console.log(response.data[0].provider.image);
          const doctor = response.data[0].provider.image;
        if(response.data.length > 0)   this.setState({"listOfProviders": response.data});
      })
  }

My html to use is:我要使用的 html 是:

  {this.state.listOfProviders.length > 0 ?
                      <tbody>
                        {this.state.listOfProviders.map((providerData,indx)=>
                          <tr>
                            <td className="font-weight-medium"> <img className="logo" src={doctor} alt="pam-logo" /> {providerData.provider.firstName} {providerData.provider.lastName} </td>
                            <td>{providerData.provider.email}</td>
                            <td>{providerData.provider.phoneNumber}</td>
                            <td>{(providerData.provider.specialities)}</td>
                            <td > {providerData.provider.qbid}</td>
                            <td> Mountain view,Ave </td>
                            <td><Icon icon={editIcon} width="20px"/>&nbsp;&nbsp;<Icon icon={deleteIcon} width="20px"/></td>

                        </tr>
                        )}
                      </tbody>

I need image in src tag.我需要 src 标签中的图像。

I guess doctor vairable scoping problem.我猜医生变量范围界定问题。 As you are declaring this const doctor = response.data[0].provider.image;当你声明这个 const doctor = response.data[0].provider.image; in componentDidMount but you are accessing from another place.在 componentDidMount 中,但您正在从另一个地方访问。

I don't know the use case here.我不知道这里的用例。 But in my opinion, the variable declared in the componentDidMount() is not accessible in the render method.但在我看来,在 componentDidMount() 中声明的变量在 render 方法中是不可访问的。 Just declare that variable as class variable in the constructor and then you can use it using this.doctor .只需在构造函数中将该变量声明为类变量,然后您就可以使用this.doctor使用它。

Or instead of declaring the variable you can just read it's value from the iterator.或者,您可以从迭代器中读取它的值,而不是声明变量。 Like, providerData.provider.image像, providerData.provider.image

create a state variable in constructor在构造函数中创建状态变量

this.state = {
    doctor: ''
}

after geting response update the image url using setState inside componentDidMount获得响应后,在componentDidMount使用 setState 更新图像 url

this.setState({doctor: response.data[0].provider.image})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM