美文网首页
angular2关于自动获取配置路由里面titile的值《路由跳

angular2关于自动获取配置路由里面titile的值《路由跳

作者: SevenLonely | 来源:发表于2017-07-02 21:36 被阅读0次
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的值《路由跳

      本文链接:https://www.haomeiwen.com/subject/zrdocxtx.html