1- import { Component } from "@angular/core" ;
1+ import { Component , Inject , inject } from "@angular/core" ;
22import { RouterOutlet } from "@angular/router" ;
33import { HeaderComponent } from "./components/header.component" ;
44import { FooterComponent } from "./components/footer.component" ;
5+ import { Subscription } from "rxjs" ;
6+ import { BlogInfo } from "./models/blog-info" ;
7+ import { BlogService } from "./services/blog.service" ;
8+ import { DOCUMENT } from "@angular/common" ;
59
610
711@Component ( {
@@ -26,4 +30,42 @@ import { FooterComponent } from "./components/footer.component";
2630 ` ,
2731 ] ,
2832} )
29- export class AppComponent { }
33+ export class AppComponent {
34+ title = "angular-material-app" ;
35+ blogURL ! : string ;
36+ blogInfo ! : BlogInfo ;
37+ siteFavicon : any ;
38+ blogService : BlogService = inject ( BlogService ) ;
39+ private querySubscription ?: Subscription ;
40+
41+ constructor ( @Inject ( DOCUMENT ) private document : Document ) { }
42+
43+ ngOnInit ( ) : void {
44+ this . blogURL = this . blogService . getBlogURL ( ) ;
45+ this . siteFavicon = this . document . querySelector (
46+ 'link[rel="icon"]'
47+ ) as HTMLLinkElement ;
48+ this . querySubscription = this . blogService
49+ . getBlogInfo ( this . blogURL )
50+ . subscribe ( ( data ) => {
51+ this . blogInfo = data ;
52+ if ( this . blogInfo . isTeam && this . blogInfo . favicon ) {
53+ this . siteFavicon . href = this . blogInfo . favicon ;
54+ } else {
55+ this . siteFavicon . href = "favicon.ico" ;
56+ }
57+ if ( ! this . blogInfo . isTeam ) {
58+ this . blogService . getAuthorInfo ( this . blogURL ) . subscribe ( ( data ) => {
59+ if ( data . profilePicture ) {
60+ this . siteFavicon . href = data . profilePicture ;
61+ } else {
62+ this . siteFavicon . href = "favicon.ico" ;
63+ }
64+ } ) ;
65+ }
66+ } ) ;
67+ }
68+ ngOnDestroy ( ) : void {
69+ this . querySubscription ?. unsubscribe ( ) ;
70+ }
71+ }
0 commit comments