0

我正在使用 angular 5 并遇到问题。我想动态更改我的页面标题。我已经这样做了但是我没有在页面标题后添加后缀。假设我的页面标题 Home 并且我想添加后缀为 Home | 我的标题。请帮我解决这个问题。

这是我的代码..

app.component.ts file

 // imported 

 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';

 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']));



my routing file

{
 path: '', 
 component: HomeComponentComponent, 
 pathMatch: 'full', 
 data: { title: 'Home' }  
},

动态页面标题工作正常。我想在页面标题后添加后缀作为主页 | 我的标题请帮我解决这个问题...

4

1 回答 1

1

代替:

.subscribe((event) => this.titleService.setTitle(event['title']));

.subscribe((event) => this.titleService.setTitle(`${event['title']} | ${myTitle}`));

或创建自己的myTitleService包装角标题服务,以便当您这样做时

this.myTitleService.setTitle(event['title']));

它会调用

this.titleService.setTitle(`${title} | ${globalTitle}`);

于 2018-01-21T09:10:49.163 回答