回答
标题可以使用Title
service
要想从目前的标题设置可以使用data
路由属性。
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;
}
}
刚刚发现https://github.com/angular/angular/issues/9662#issuecomment-229034288其中表现出了类似的方法。
我还发现https://toddmotto.com/dynamic-page-titles-angular-2-router-events有点漂亮的代码。
下面的代码(摘自:https://toddmotto.com/dynamic-page-titles-angular-2-router-events)的作品就像一个魅力:
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' } }
]
}];
然后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']));
}
}
这是一个非常长的代码行来获取数据:路径 –
的{title}不适用于嵌套/链接标题(ParentRoute title [separator] ChildRoute title)。 接受的答案是完整的解决方案。 – MrCroft
它是否适用于加载'{path:':shipmentId',canActivate:[SessionGuardService],loadChildren:'app/edit-shipment/edit-shipment.module#EditShipmentModule',data:{title:PageTitle.editShipmentPage}}' ,注意到当定义的路由来自延迟加载时标题不会被设置 –
- 1. 使用Angular 2路由器服务更改页面标题
- 2. Angular 2路由器刷新页面,同时路由
- 3. Angular 2:更新路由器url获取更改时的iframe src
- 4. 使用angularjs路由更改页面的标题
- 5. 刷新页面角2路由器
- 6. 订阅新的Angular 2路由器的更改(> = rc1)
- 7. 在路由器事件中显示Angular 2的翻译动态页面标题
- 8. 页标题,描述使用angular-ui路由器
- 9. 如何更改Angular 2目录路由?
- 10. Angular 2路由问题
- 11. Angular 2路由问题
- 12. Angular 2路由问题,路由到空白页
- 13. Angular 2路由 - 路由器RC3
- 14. Angular 2使用管道路由到页面
- 15. 如何使用$ routeProvider更改Angular中的页面标题
- 16. 角2路由器标题与参数
- 17. Angular 2路由器错误
- 18. UI路由器刷新页面而不是状态更改
- 19. Angular 2渲染页面没有路由器插座
- 20. Angular Ui路由器 - 无法更改$ state.current.data
- 21. Angular UI路由器,标题动态
- 22. Angular 2单击事件:更改图像,然后更改路由
- 23. 在Angular中路由到非Angular页面
- 24. Angular 2根据当前路由器状态更改sidenav内容
- 25. 如何强制路由器更新Angular 2中的当前路由?
- 26. 如何获取Angular UI路由器在页面加载路由
- 27. 使用Angular 2.0路由器进行页面过渡动画
- 28. 更新角度后路由器不会路由到组件2
- 29. Angular 2路由,.htaccess
- 30. Angular 2路由onActivate
什么是第一次加载? –
有什么问题? –
router.events.subscribe ...对路由器的改变非常有用,但是它并没有在第一次加载页面时被触发,所以没有标题 –