は私のコンストラクタエラーが発生します。ここ角度2コンポーネント内の名前空間をインポートすると、私のコンストラクタエラーを与える
ERROR Error: Uncaught (in promise): TypeError: YT.Player is not a constructor
TypeError: YT.Player is not a constructor
は、この名前空間に
を参照している私の角度成分の定義であり、私はまた、私のコンポーネントで/// <reference path
を使用しようとしましたが、これは役に立ちません。クローム/デベロッパーツールではエラーが発生します。私はng build
またはng serve
を実行することができ、それは正常に構築/提供されます。ここで
import {
Component,
OnInit
} from '@angular/core';
@Component({
selector: 'app-video',
templateUrl: './video.component.html',
styleUrls: ['./video.component.css']
})
export class VideoComponent implements OnInit {
private id: string = 'qDuKsiwS5xw';
p: YT.Player;
done = false;
constructor() {
this.p = new YT.Player('player', {
height: 390,
width: 640,
videoId: 'M7lc1UVf-VE',
events: {
'onReady': this.onPlayerReady,
'onStateChange': this.onStateChange
}
});
}
ngOnInit() {
}
onStateChange(event) {
console.log('player state', event.data);
}
// 4. The API will call this function when the video player is ready.
onPlayerReady(event) {
event.target.playVideo();
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING && !this.done) {
setTimeout(this.stopVideo, 6000);
this.done = true;
}
}
stopVideo() {
this.p.stopVideo();
}
}
は私のtsconfig.app.jsonファイルであると私はあなたがNG2のユーチューブプレーヤーを使用する必要がDefinitelyTyped
http://definitelytyped.org/docs/youtube--youtube/classes/yt.player.html
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"baseUrl": "./",
"module": "es2015",
"types": ["youtube"]
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
YT.Playerをインポートする必要はありませんか?そして、私は、人生を楽にするためにインストールする角度のあるYouTubeプレーヤーパッケージを見つけようとしています。たとえば、https://github.com/orizens/ng2-youtube-playerを試してみましたか?私はこれを自分で使っていない。しかし、私はいつも最も抵抗のない道を試みています! – JGFMK