1- import { Component } from "@angular/core" ;
1+ import { Component , OnInit } from '@angular/core' ;
2+ import {
3+ Router ,
4+ ActivatedRoute ,
5+ NavigationEnd ,
6+ RouterEvent
7+ } from '@angular/router' ;
8+ import { filter , map , mergeMap } from 'rxjs/operators' ;
9+ import { SeoService } from './services/seo.service' ;
210
311interface Menu {
412 title : string ;
@@ -7,31 +15,58 @@ interface Menu {
715}
816
917@Component ( {
10- selector : " app-root" ,
11- templateUrl : " ./app.component.html" ,
12- styleUrls : [ " ./app.component.scss" ]
18+ selector : ' app-root' ,
19+ templateUrl : ' ./app.component.html' ,
20+ styleUrls : [ ' ./app.component.scss' ]
1321} )
14- export class AppComponent {
22+ export class AppComponent implements OnInit {
1523 menus : Menu [ ] = [
1624 {
17- title : " Home" ,
18- link : "/" ,
25+ title : ' Home' ,
26+ link : '/' ,
1927 options : { exact : true }
2028 } ,
2129 {
22- title : " Operators" ,
23- link : " /operators" ,
30+ title : ' Operators' ,
31+ link : ' /operators' ,
2432 options : { exact : false }
2533 } ,
2634 {
27- title : " Companies" ,
28- link : " /companies" ,
35+ title : ' Companies' ,
36+ link : ' /companies' ,
2937 options : { exact : false }
3038 } ,
3139 {
32- title : " Team" ,
33- link : " /team" ,
40+ title : ' Team' ,
41+ link : ' /team' ,
3442 options : { exact : false }
3543 }
3644 ] ;
45+
46+ constructor (
47+ private _router : Router ,
48+ private _activatedRoute : ActivatedRoute ,
49+ private _seo : SeoService
50+ ) { }
51+
52+ ngOnInit ( ) {
53+ this . _router . events
54+ . pipe (
55+ filter ( ( e : RouterEvent ) => e instanceof NavigationEnd ) ,
56+ map ( ( ) => {
57+ let route = this . _activatedRoute ;
58+ while ( route . firstChild ) {
59+ route = route . firstChild ;
60+ }
61+ return route ;
62+ } ) ,
63+ filter ( route => route . outlet === 'primary' ) ,
64+ mergeMap ( route => route . data )
65+ )
66+ . subscribe ( data => {
67+ if ( data !== { } ) {
68+ this . _seo . setHeaders ( data . title || [ ] , data . description || '' ) ;
69+ }
70+ } ) ;
71+ }
3772}
0 commit comments