Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从Spotify Response中反应背景图像_Javascript_Reactjs_Spotify - Fatal编程技术网

Javascript 从Spotify Response中反应背景图像

Javascript 从Spotify Response中反应背景图像,javascript,reactjs,spotify,Javascript,Reactjs,Spotify,您好,我是ReactJs的初学者:( 所以我想从一个响应中获得背景图像(来自Spotify) 我有我想要的所有数据,但没有背景 我的代码如下所示: getNowPlaying(){ SpotifyWebApi.getMyCurrentPlaybackState() .then((Response) => { if (Response !== "") {this.setState({ nowPlaying:{ name: Response.item.name,

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

所以我想从一个响应中获得背景图像(来自Spotify)

我有我想要的所有数据,但没有背景

我的代码如下所示:

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 !")}

})


{this.state.nowPlaying.name}
{this.state.nowPlaying.artiste}


谢谢:)

要从api response在reactJs中设置背景图像,您应该使用以下内联样式设置背景图像:

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

...

对于BG图像(不工作)的Img(工作)在web浏览器上的响应可能重复@TomSita请注意我示例中的样式属性
<div 
  className="background" 
  style={{backgroundImage: `url(${this.state.nowPlaying.backgroundImage})`}}
>
...
</div>