angular2关于自动获取配置路由里面titile的值《路由跳转,页面title变化》

import {Component, OnInit} from '@angular/core';
import {Router, NavigationEnd, ActivatedRoute} from '@angular/router';
import {Title} from '@angular/platform-browser';

import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';

@Component({
  selector: 'app',
  templateUrl: './app.component.html',
})
export class AppComponent implements OnInit {
  constructor(public router: Router, public activatedRoute: ActivatedRoute, public title: Title) {

  }

  ngOnInit() {
    this.router.events
      .filter(event => event instanceof NavigationEnd)
      .map(() => this.activatedRoute)
      .map(route => {
        while (route.firstChild) route = route.firstChild;
        return route;
      })
      .filter(route => route.outlet === 'primary')
      .mergeMap(route => route.data)
      .subscribe((event) => this.title.setTitle(event['title']));
  }
}

路由设置

  { path: 'login', component: LoginComponent ,data: {'title': '登录页面'}},

你可能感兴趣的:(angular2关于自动获取配置路由里面titile的值《路由跳转,页面title变化》)