@@ -20,31 +20,33 @@ If your video is found at https://www.youtube.com/watch?v=PRQCAL_RMVo, then your
20
20
21
21
``` typescript
22
22
// example-module.ts
23
- import {NgModule } from ' @angular/core' ;
23
+ import {NgModule , Component , OnInit } from ' @angular/core' ;
24
24
import {YouTubePlayerModule } from ' @angular/youtube-player' ;
25
25
26
26
@NgModule ({
27
- imports: [
28
- YouTubePlayerModule ,
29
- ],
27
+ imports: [YouTubePlayerModule ],
30
28
declarations: [YoutubePlayerExample ],
31
29
})
32
30
export class YoutubePlayerExampleModule {
33
31
}
34
32
33
+ let apiLoaded = false ;
34
+
35
35
// example-component.ts
36
36
@Component ({
37
37
template: ' <youtube-player videoId="PRQCAL_RMVo"></youtube-player>' ,
38
38
selector: ' youtube-player-example' ,
39
39
})
40
40
class YoutubePlayerExample implements OnInit {
41
41
ngOnInit() {
42
- // This code loads the IFrame Player API code asynchronously, according to the instructions at
43
- // https://developers.google.com/youtube/iframe_api_reference#Getting_Started
44
- const tag = document .createElement (' script' );
45
-
46
- tag .src = " https://www.youtube.com/iframe_api" ;
47
- document .body .appendChild (tag );
42
+ if (! apiLoaded ) {
43
+ // This code loads the IFrame Player API code asynchronously, according to the instructions at
44
+ // https://developers.google.com/youtube/iframe_api_reference#Getting_Started
45
+ const tag = document .createElement (' script' );
46
+ tag .src = ' https://www.youtube.com/iframe_api' ;
47
+ document .body .appendChild (tag );
48
+ apiLoaded = true ;
49
+ }
48
50
}
49
51
}
50
52
0 commit comments