Skip to content

Instantly share code, notes, and snippets.

@twerske
Created May 13, 2022 17:36
Show Gist options
  • Save twerske/16f4d998c5b87c589cd2b6ec015f8732 to your computer and use it in GitHub Desktop.
Save twerske/16f4d998c5b87c589cd2b6ec015f8732 to your computer and use it in GitHub Desktop.
Streamlined page title accessibility with a TitleStrategy
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