Created
May 13, 2022 17:36
-
-
Save twerske/16f4d998c5b87c589cd2b6ec015f8732 to your computer and use it in GitHub Desktop.
Streamlined page title accessibility with a TitleStrategy
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
const routes: Routes = [{ | |
path: 'home', | |
component: HomeComponent | |
}, { | |
path: 'about', | |
component: AboutComponent, | |
title: 'About Me' // <-- Page title | |
}]; | |
@Injectable() | |
export class TemplatePageTitleStrategy extends TitleStrategy { | |
override updateTitle(routerState: RouterStateSnapshot) { | |
const title = this.buildTitle(routerState); | |
if (title !== undefined) { | |
document.title = `My App - ${title}`; | |
} else { | |
document.title = `My App - Home`; | |
}; | |
}; | |
@NgModule({ | |
… | |
providers: [{provide: TitleStrategy, useClass: TemplatePageTitleStrategy}] | |
}) | |
class MainModule {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment