我在 Angular 10 项目中为 Azure 媒体播放器设置打字稿定义时遇到问题。我正在使用文档中的 *.d.ts文件
我尝试使用 tsconfig.json 文件中的 typeRoots 设置定义:
"typeRoots": [ "./src/types/", "./node_modules/@types"],
我发现有冲突的教程要求包含类型的文件夹要么位于根级别,要么位于 src 文件夹内。现在,azuremediaplayer.d.ts 文件位于./src/types/azuremediaplayer/
.
在 tsconfig 文件中,有一个错误说Cannot find type definition file for 'azuremediaplayer'.
在我的组件中,我从官方文档中复制粘贴了代码,没有函数(抛出错误)。
ngOnInit(): void {
var myPlayer = amp('vid1', {
"nativeControlsForTouch": false,
autoplay: false,
controls: true,
width: "640",
height: "400",
poster: ""
});
myPlayer.src([{
src: "http://amssamples.streaming.mediaservices.windows.net/91492735-c523-432b-ba01-faba6c2206a2/AzureMediaServicesPromo.ism/manifest",
type: "application/vnd.ms-sstr+xml"
}])
}
如果我 ctrl+click 上面代码中的 amp,我会被带到 .d.ts 文件,但函数调用中的一些参数会抛出错误:
Argument of type '{ nativeControlsForTouch: boolean; autoplay: false; controls: true; width: string; height: string; poster: string; }' is not assignable to parameter of type 'Options'.
Object literal may only specify known properties, and '"nativeControlsForTouch"' does not exist in type 'Options'.
初始化时没有出现错误是这样的:
var myPlayer = amp('vid1', {
autoplay: false,
controls: true,
poster: ""
});
myPlayer.src([{
src: "http://amssamples.streaming.mediaservices.windows.net/91492735-c523-432b-ba01-faba6c2206a2/AzureMediaServicesPromo.ism/manifest",
type: "application/vnd.ms-sstr+xml"
}])
}
如果播放器在 html 文件中实例化,则播放器可以工作。
我也尝试了这个答案中描述的步骤:如果我用 引用文件/// <reference path="../../../types/azuremediaplayer/azuremediaplayer.d.ts" />
,没有任何变化,但 json 中没有错误。如果我在我的files: []
属性中添加文件,amp()
则无法识别。如果我添加"include": ["src/**/*"]
,则与使用 typeRoot 相同,但错误相同。
我应该如何设置播放器以使其正常工作?