2016-07-28 13 views

cevap

36

data özelliğinin kullanılması kullanılabilir.

const routes: RouterConfig = [ 
    { 
     path: '', 
     redirectTo: '/login', 
     pathMatch: 'full', 
    }, 
    { 
     path: 'login', 
     component: LoginComponent, 
     data: {title: 'Login'} 
    }, 
    { 
     path: 'home', 
     component: HomeComponent, 
     data: {title: 'Home'} 
    }, 
    { 
     path: 'wepays', 
     component: WePaysComponent, 
     data: {title: 'WePays'} 
    } 
]; 
export class AppComponent { 
    constructor(titleService:Title, router:Router, activatedRoute:ActivatedRoute) { 
    router.events.subscribe(event => { 
     if(event instanceof NavigationEnd) { 
     var title = this.getTitle(router.routerState, router.routerState.root).join('-'); 
     console.log('title', title); 
     titleService.setTitle(title); 
     } 
    }); 
    } 

    // collect that title data properties from all child routes 
    // there might be a better way but this worked for me 
    getTitle(state, parent) { 
    var data = []; 
    if(parent && parent.snapshot.data && parent.snapshot.data.title) { 
     data.push(parent.snapshot.data.title); 
    } 

    if(state && parent) { 
     data.push(... this.getTitle(state, state.firstChild(parent))); 
    } 
    return data; 
    } 
} 

Plunker example

sadece benzer bir yaklaşım gösterilmektedir https://github.com/angular/angular/issues/9662#issuecomment-229034288 bulundu.

Ayrıca daha güzel bir kod ile https://toddmotto.com/dynamic-page-titles-angular-2-router-events da buldum. (Alınan: https://toddmotto.com/dynamic-page-titles-angular-2-router-events) kodu takiben

+0

İlk kez yükleme süresi nedir? –

+0

Sorun nedir? –

+0

yönlendirici.events.subscribe ... yönlendirici değişikliği için harika çalışıyor, ancak ilk kez sayfa yükü için tetiklenmiyor, bu nedenle hiçbir başlık –

10

bir cazibe gibi çalışır: o zaman

const routes: Routes = [{ 
    path: 'calendar', 
    component: CalendarComponent, 
    children: [ 
    { path: '', redirectTo: 'new', pathMatch: 'full' }, 
    { path: 'all', component: CalendarListComponent, data: { title: 'My Calendar' } }, 
    { path: 'new', component: CalendarEventComponent, data: { title: 'New Calendar Entry' } }, 
    { path: ':id', component: CalendarEventComponent, data: { title: 'Calendar Entry' } } 
    ] 
}]; 

ve sonraki çağrılarda AppComponent

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'])); 
    } 
} 
bir bileşen olarak adlandırılır ilk kez çalışacak
+3

sadece verileri almak için gerçekten çok uzun kodlar dizisidir: –

+0

yolunun {title}, yuvalama/zincirleme başlıkları ile çalışmaz (ParentRoute title [separator] ChildRoute başlığı). Kabul edilen cevap tam çözümdür. – MrCroft

+0

Yüklemek için çalışıyor mu? {Yol: ': shipmentId', canActivate: [SessionGuardService], loadChildren: 'app/edit-shipment/edit-shipment.module # EditShipmentModule', data: {title: PageTitle.editShipmentPage}} ' , Tanımlanan rota, tembel yükten geliyorsa başlığın ayarlanmadığını fark ettiniz. –