npm Angular :“参考误差:当使用“@types/spotify-web-playback-sdk”时,未定义Spotify

jv4diomz  于 2023-01-09  发布在  Angular
关注(0)|答案(1)|浏览(165)

我尝试在一个有Angular 的项目中使用spotify的webplayer sdk,我遵循了this question and answer中给出的步骤:* 安装types包,然后将types引用添加到使用Spotify namespace的文件的开头 *

npm install --save @types/spotify-web-playback-sdk

以及

///  <reference types="@types/spotify-web-playback-sdk"/>

作为文件的第一行。
我的代码如下所示

//class variable
spotifyPlayer: Spotify.Player | null = null

以及

createWebPlayer(){
    const accessToken = this.authObject.access_token
    this.spotifyPlayer = new Spotify.Player({ //line 107
      name: 'Web Playback SDK Quick Start Player',
      getOAuthToken: cb => {
        cb(accessToken)
      },
      volume: 0.5
    })

    //ready
    this.spotifyPlayer!.addListener('ready', ({ device_id }) => {
      console.log('Ready with Device ID', device_id);
    });
  
    // Not Ready
    this.spotifyPlayer!.addListener('not_ready', ({ device_id }) => {
      console.log('Device ID has gone offline', device_id);
    });
}

编译器对此没有问题,但是当我调用createWebPlayer()时,我得到了错误

ERROR ReferenceError: Spotify is not defined
    createWebPlayer home.component.ts:107

我是手动调用这个函数的,而且是在获得访问令牌之后调用的,所以这应该不是问题;我真的被这个难住了。
"我所尝试的"
我尝试添加一个import语句,如下所示

import * as Spotify from 'spotify-web-playback-sdk'

但我得到的错误是它不是一个模块。我所做的唯一安装是运行

npm install --save @types/spotify-web-playback-sdk

我还试着把107号线

this.spotifyPlayer = new window.Spotify.Player({

但也会产生类似的误差

ERROR ReferenceError: window.Spotify is not defined
    createWebPlayer home.component.ts:107

我又一次被难住了,我觉得我错过了一些明显的东西。任何帮助都是很好的!!

d4so4syb

d4so4syb1#

好了,我修正了这个问题(我想)。从this angular project (github link)我发现我需要添加脚本到html页面。我猜类型文件只是为了避免类型错误,并没有实际链接到代码?
我从该项目复制的代码是:

onst script = document.createElement('script');
script.src = 'https://sdk.scdn.co/spotify-player.js';
script.type = 'text/javascript';
script.addEventListener('load', (e) => {
  console.log(e);
});
document.head.appendChild(script);
window.onSpotifyWebPlaybackSDKReady = () => {
  console.log('The Web Playback SDK is ready. We have access to Spotify.Player');

我当前的代码如下所示:

createWebPlayerTwo () {
const script = document.createElement('script')
script.src = 'https://sdk.scdn.co/spotify-player.js'
script.type = 'text/javascript'
script.addEventListener('load', e => {
  console.log(e)
})
document.head.appendChild(script)
window.onSpotifyWebPlaybackSDKReady = () => {
  console.log(
    'The Web Playback SDK is ready. We have access to Spotify.Player'
  )
  const accessToken = this.authObject.access_token
  this.spotifyPlayer = new Spotify.Player({
    name: 'Web Playback SDK Quick Start Player',
    getOAuthToken: cb => {
      cb(accessToken)
    },
    volume: 0.5
  })

  this.spotifyPlayer.addListener('ready', ({ device_id }) => {
    console.log('Ready with Device ID', device_id)
  })

  this.spotifyPlayer.addListener('not_ready', ({ device_id }) => {
    console.log('Device ID has gone offline', device_id)
  })

  this.spotifyPlayer.addListener('initialization_error', ({ message }) => {
    console.error(message)
  })

  this.spotifyPlayer.addListener('authentication_error', ({ message }) => {
    console.error(message)
  })

  this.spotifyPlayer.addListener('account_error', ({ message }) => {
    console.error(message)
  })

  this.spotifyPlayer.connect()
  console.log('hi')
}

}
而且它很有效:)..
我收到消息“设备ID 13b20fd9就绪...”

相关问题