how to change page title in angular2 router

后端 未结 14 1289
深忆病人
深忆病人 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:08

    Here's the simplest way to change the Title of the page, when pages/views are navigated (Tested as of Angular @2.3.1). Simply apply the following solution to all the views you have and you should be good to go:

    Example Code in About Us page/view:

    import {Title} from "@angular/platform-browser";
    
    export class AboutUsComponent implements OnInit {
    
      constructor(private _titleService: Title) {
      }
    
      ngOnInit() {
        //Set page Title when this view is initialized
        this._titleService.setTitle('About Us');
      }
    
    }
    

    Example Code in Contact Us page/view:

    import {Title} from "@angular/platform-browser";
    
    export class ContactusComponent implements OnInit {
    
      constructor(private _titleService: Title) {
      }
    
      ngOnInit() {
        //Set page Title
        this._titleService.setTitle('Contact Us');
      }
    
    }
    

提交回复
热议问题