Last active
July 26, 2019 08:07
-
-
Save CryceTruly/64e1b3b8cce18334f3aed077817b3bec to your computer and use it in GitHub Desktop.
Setting dynamic page titles in an angular 2+ app.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Step 1 #Provide a title in the route configuration,Usually in the app.module.ts | |
{ path: '', component: HomeComponent, data: { title: 'welcome' } }, | |
Step 2 #import {ActivatedRoute } from '@angular/router'; | |
Step 3 #Inject the ActivatedRoute interface in the constructor of your component: | |
constructor(private router:ActivatedRoute) { } | |
Step 4,#Subscribe to the data observable and pull out your data variables,use them for what you want. | |
ngOnInit() { | |
this.router.data.subscribe(data=> { | |
# This does the job: | |
document.title=data['title']; | |
} | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment