【问题标题】:React backgroundImage from Spotify Response来自 Spotify 响应的背景图像反应
【发布时间】:2018-06-17 09:30:29
【问题描述】:

您好,我是 ReactJs 的初学者 :(

所以我想要来自响应(来自 Spotify)的背景图片

我有我想要的所有数据,但没有 BG

我的代码看起来像:

getNowPlaying(){
SpotifyWebApi.getMyCurrentPlaybackState()
.then((Response) => {
  if (Response !== "") {this.setState({
    nowPlaying:{
      name: Response.item.name,
      image: Response.item.album.images[0].url,
      artiste: Response.item.artists[0].name,
      backgroundImage: 'url(Response.item.album.images[0].url)'
    }
    })


  }else{
    alert("Play music !");
    console.log("Play music !")}

})

<div className="box1"> 
<img src={ this.state.nowPlaying.image} /> 
</div>

<div className="box2">
<h1>{ this.state.nowPlaying.name} </h1>
<p>{ this.state.nowPlaying.artiste} </p> 
</div>
<div className="background" style={this.state.nowPlaying.backgroundImage}></div>

谢谢:)

【问题讨论】:

标签: javascript reactjs spotify


【解决方案1】:

要通过 api 响应在 reactJs 中设置背景图像,您应该使用内联样式设置背景图像,如下所示:

<div 
  className="background" 
  style={{backgroundImage: `url(${this.state.nowPlaying.backgroundImage})`}}
>
...
</div>

【讨论】:

  • 我在网络浏览器上收到来自 Img(工作)的此响应 i.scdn.co/image/d437791a5159ac23270b8850c307c7da98e246cb"> 对于 BG 图像(不工作)
  • @TomSita 请注意我示例中的样式属性
猜你喜欢
  • 2015-12-22
  • 2014-04-28
  • 1970-01-01
  • 1970-01-01
  • 2018-05-04
  • 2021-01-25
相关资源
最近更新 更多