Javascript 从Spotify Response中反应背景图像
您好,我是ReactJs的初学者:( 所以我想从一个响应中获得背景图像(来自Spotify) 我有我想要的所有数据,但没有背景 我的代码如下所示: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,
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>