javascript - Angular:使用 @types/spotify-web-playback-sdk 时找不到命名空间 "Spotify"

标签 javascript angular typescript spotify

我正在尝试使用 https://www.npmjs.com/package/@types/spotify-web-playback-sdk在我的 Angular 项目中,在我的组件之一中输入 Spotify 播放器。我按照说明安装了类型...

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

当我定义此类变量并将其输入到 SpotifyPlayer 时,Visual Studio 代码 linter 不会提示...

// class variable
spotifyPlayer: Spotify.SpotifyPlayer;

但是,当我去构建代码时,我收到此错误......

ERROR in src/app/components/header/header.component.ts:32:18 - error TS2503: Cannot find namespace 'Spotify'.

32   spotifyPlayer: Spotify.SpotifyPlayer;

我正在使用...

Angular CLI: 9.1.0
Node: 10.15.1
OS: win32 x64

Angular: 9.1.0
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.901.0
@angular-devkit/build-angular     0.901.0
@angular-devkit/build-optimizer   0.901.0
@angular-devkit/build-webpack     0.901.0
@angular-devkit/core              9.1.0
@angular-devkit/schematics        9.1.0
@ngtools/webpack                  9.1.0
@schematics/angular               9.1.0
@schematics/update                0.901.0
rxjs                              6.5.4
typescript                        3.8.3
webpack                           4.42.0

感谢您的帮助!

最佳答案

根据类型定义here spotify-web-playback-sdk非 npm 包。 根据 Spotify 文档 here ,您将直接在 html 中添加 JS 包,这意味着此 Script 不会作为 模块 加载>.

来自DefinitelyTyped

You may need to add a types reference if you're not using modules

您可以将以下行添加到使用 Spotify 命名空间 的文件顶部,错误就会消失。

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

更多信息是herehere 。每当 Spotify 发布 npm 库时,他们更喜欢使用它。

关于javascript - Angular:使用 @types/spotify-web-playback-sdk 时找不到命名空间 "Spotify",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61039624/

相关文章:

javascript - 从 javascript [Angular JS] 访问 JSP 变量

javascript - 在 Angular 2 项目中添加 .js 脚本

javascript - Chrome 扩展程序 : create new window in depends on window presence

javascript - 在特定的 div 上打开一个页面

angular - 如何手动触发事件并进行变更检测?

Angular 2 - 在服务中设置超时

javascript - 如何在悬停时将下拉内容放置在左侧导航的右侧

javascript - 如何使用 Typescript 对具有后备值的解构对象进行类型检查?

node.js - angularfire2安装和设置期间出现错误

javascript - jsPlumb 连接器不可拖动