1- import { Component } from '@angular/core' ;
1+ import { Component , OnInit , OnDestroy , inject , Inject } from '@angular/core' ;
22import { RouterOutlet } from '@angular/router' ;
3+ import { Subscription } from 'rxjs' ;
4+ import { BlogInfo } from './models/blog-info' ;
5+ import { BlogService } from './services/blog.service' ;
6+ import { ThemeService } from './services/theme.service' ;
7+ import { DOCUMENT } from '@angular/common' ;
38
49@Component ( {
510 selector : 'app-root' ,
@@ -8,6 +13,42 @@ import { RouterOutlet } from '@angular/router';
813 templateUrl : './app.component.html' ,
914 styleUrl : './app.component.scss'
1015} )
11- export class AppComponent {
16+ export class AppComponent implements OnInit , OnDestroy {
1217 title = 'angular-primeng-app' ;
18+ blogURL ! : string ;
19+ blogInfo ! : BlogInfo ;
20+ siteFavicon : any ;
21+ themeService : ThemeService = inject ( ThemeService ) ;
22+ blogService : BlogService = inject ( BlogService ) ;
23+ private querySubscription ?: Subscription ;
24+
25+ constructor ( @Inject ( DOCUMENT ) private document : Document ) { }
26+
27+ ngOnInit ( ) : void {
28+ this . blogURL = this . blogService . getBlogURL ( ) ;
29+ this . siteFavicon = this . document . querySelector ( 'link[rel="icon"]' ) as HTMLLinkElement ;
30+ this . querySubscription = this . blogService
31+ . getBlogInfo ( this . blogURL )
32+ . subscribe ( ( data ) => {
33+ this . blogInfo = data ;
34+ if ( this . blogInfo . isTeam && this . blogInfo . favicon ) {
35+ this . siteFavicon . href = this . blogInfo . favicon ;
36+ } else {
37+ this . siteFavicon . href = "favicon.ico" ;
38+ }
39+ if ( ! this . blogInfo . isTeam ) {
40+ this . blogService . getAuthorInfo ( this . blogURL ) . subscribe ( ( data ) => {
41+ if ( data . profilePicture ) {
42+ this . siteFavicon . href = data . profilePicture ;
43+ } else {
44+ this . siteFavicon . href = "favicon.ico" ;
45+ }
46+ } ) ;
47+ }
48+ } ) ;
49+ }
50+
51+ ngOnDestroy ( ) : void {
52+ this . querySubscription ?. unsubscribe ( ) ;
53+ }
1354}
0 commit comments