码迷,mamicode.com
首页 > 其他好文 > 详细

[Angular2 Router] Setup page title with Router events

时间:2016-11-25 07:07:38      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:component   lte   title   nts   ref   cto   ons   imp   route   

Article

 

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

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

@Component({...})
export class AppComponent implements OnInit {
  constructor(
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private titleService: 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.titleService.setTitle(event[title]));
  }
}

 

[Angular2 Router] Setup page title with Router events

标签:component   lte   title   nts   ref   cto   ons   imp   route   

原文地址:http://www.cnblogs.com/Answer1215/p/6100169.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!