how to change page title in angular2 router

后端 未结 14 1303
深忆病人
深忆病人 2020-12-04 16:17

I am trying to change the page title from the router, can this be done?

import {RouteConfig} from \'angular2/router\';
@RouteConfig([
  {path: \'/home\', com         


        
14条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 17:13

    Angular 2 provides a Title Service see Shailesh answer is just copy of that code.

    I our app.module.ts

    import { BrowserModule, Title } from '@angular/platform-browser';
    ........
    providers: [..., Title],
    bootstrap: [AppComponent]
    

    Now move to our app.component.ts

    import { Title }     from '@angular/platform-browser';
    ......
    export class AppComponent {
    
        public constructor(private titleService: Title ) { }
    
        public setTitle( newTitle: string) {
          this.titleService.setTitle( newTitle );
        }
    }
    

    Put the title tag on your component html and it will read and set for you.

    If you want to know how to set it dynamically and further detail see this article

提交回复
热议问题