最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Angular 2 利用Router事件和Title实现动态页面标题的方法

来源:懂视网 责编:小采 时间:2020-11-27 22:31:46
文档

Angular 2 利用Router事件和Title实现动态页面标题的方法

Angular 2 利用Router事件和Title实现动态页面标题的方法:Angular2 为我们提供了名为Title的Service用于修改和获取页面标题,但是如果只是能够在每个页面的ngOnInit方法中为每个页面设置标题岂不是太low了,不符合Angular2高(zhuang)大(bi)的身影。我们想要的结果是在页面改变时能够动态地改变页面标题,如此最好的解
推荐度:
导读Angular 2 利用Router事件和Title实现动态页面标题的方法:Angular2 为我们提供了名为Title的Service用于修改和获取页面标题,但是如果只是能够在每个页面的ngOnInit方法中为每个页面设置标题岂不是太low了,不符合Angular2高(zhuang)大(bi)的身影。我们想要的结果是在页面改变时能够动态地改变页面标题,如此最好的解

Angular2 为我们提供了名为Title的Service用于修改和获取页面标题,但是如果只是能够在每个页面的ngOnInit方法中为每个页面设置标题岂不是太low了,不符合Angular2高(zhuang)大(bi)的身影。我们想要的结果是在页面改变时能够动态地改变页面标题,如此最好的解决方案就是组合使用Router事件和Title Service。

Title Service

使用Service自然首先要将其引入,不过要注意Title Service并不在@angular/core中,而是在@angular/platform-browser中:

import { Title } from '@angular/platform-browser';

引入之后,自然要将其注入到当前组件中,而这通常利用constructor完成:

import { Title } from '@angular/platform-browser';
import {Component} from '@angular/core';
@Component({})
export class AppComponent {
 constructor(private titleService: Title) {
 // 使用this.title到处浪
 }
}

很显然,Title Service应该有某些操作页面标题的方法,不管通过查找文档还是查找源码我们都能很容易知道其只有两个方法:

  • getTitle() 用于获取当前当前页面的标题
  • setTitle(newTitle: String) 用于设置当前页面的标题
  • 如果只是简单地静态地设置页面标题,则可以在ngOnInit方法中直接使用setTitle方法:

    // import bala...
    @Component({})
    export class AppComponent implements OnInit {
     constructor(private titleService: Title) {
     // 使用this.title到处浪
     }
    
     ngOnInit() {
     this.titleService.setTitle('New Title Here');
     }
    }
    

    在ngOnInit中使用setTitle方法设置文档标题是较好的时机,当然也可以根据自己的需求在任意地方使用setTitle方法。

    Router和Router事件

    使用Router和使用Title Service流程基本一致,先引入后注入,不过要注意Router和Title Service类似并不位于@angular/core中,而是位于@angular/router中:

    import { Title } from '@angular/platform-browser';
    import {Component} from '@angular/core';
    import {Router} from '@angular/router';
    @Component({})
    export class AppComponent {
     constructor(private titleService: Title, private router: Router) {
     // 使用this.title和this.router到处浪
     }
    }

    Router配置

    Angular2中通过URL、Router和Component之间的对应关系进行页面之间的跳转,Router把浏览器中的URL看做一个操作指南,据此可导航到一个由客户端生成的视图,并可以把参数传给支撑视图的相应组件。所以我们需要定义路由表:

    // import bala...
    export const rootRouterConfig: Routes = [
     { path: '', redirectTo: 'home', pathMatch: 'full'},
     { path: 'home', component: HomeComponent, data: {title: 'Home-Liu'} },
     { path: 'about', component: AboutComponent, data: {title: 'About-Liu'} },
     { path: 'github', component: RepoBrowserComponent,
     children: [
     { path: '', component: RepoListComponent, data: {title: 'GitHub List'} },
     { path: ':org', component: RepoListComponent,
     children: [
     { path: '', component: RepoDetailComponent, data: {title: 'Repo'} },
     { path: ':repo', component: RepoDetailComponent, data: {title: 'RepoDetail'} }
     ]
     }]
     },
     { path: 'contact', component: ContactComponent, data: {title: 'Contact-Liu'} }
    ];
     

    注意路径和组件之间的对应关系,并且为了能够在Router事件中获取到页面标题,我们在路由表中,为一些页面提供了数据data,并在data中设置了表示页面标题的title属性。

    Router事件

    利用Router事件我们就可以实现动态改变页面标题的目的,不过放置的位置很重要,我们这里选择在AppComponent的ngOnInit方法中利用subscribe订阅Router事件,因为AppComponent是根组件,所以能够订阅所有Router事件:

    ngOnInit() {
     this.router.events
     .subscribe((event) => {
     console.log(event); // 包括NavigationStart, RoutesRecognized, NavigationEnd
     });
    }

    当然我们这里这对NavigationEnd事件感兴趣:

    import {ActivatedRoute} from '@angular/router';
    // import bala...
    
    // other codes
    
    ngOnInit() {
     this.router.events
     .subscribe((event) => {
     if (event instanceof NavigationEnd) {
     console.log('NavigationEnd:', event);
     }
     });
    }
    
    

    当然使用这种判断筛选的方式并没有错,但是在现在的前端世界里显得不够优雅,我们应该使用RxJS中的filter达到我们的目的:

    import 'rxjs/add/operator/filter';
    // import bala...
    
    // other codes
    
    ngOnInit() {
     this.router.events
     .filter(event => event instanceof NavigationEnd) // 筛选原始的Observable:this.router.events
     .subscribe((event) => {
     console.log('NavigationEnd:', event);
     });
    }
    
    

    当然,我们如果想要动态改变某个页面的标题,就需要获取到当前被展示的页面对应的路由信息,而这可以通过ActivatedRoute得到,其使用方式和Title Service及Router类似,不再赘述:

    import { Title } from '@angular/platform-browser';
    import {Component, OnInit} from '@angular/core';
    import {Router, NavigationEnd, ActivatedRoute} from '@angular/router';
    import 'rxjs/add/operator/filter';
    import 'rxjs/add/operator/map';
    @Component({})
    export class AppComponent implements OnInit {
     constructor(private titleService: Title, private router: Router, private activatedRoute: ActivatedRoute) {
     // 使用this.title和this.router和this.activatedRoute到处浪
     }
    
     ngOnInit() {
     this.router.events
     .filter(event => event instanceof NavigationEnd)
     .map(() => this.activatedRoute) // 将filter处理后的Observable再次处理
     .subscribe((event) => {
     console.log('NavigationEnd:', event);
     });
     }
    }
    
    

    注意这里我们又使用了RxJS中的map来更优雅地达成我们目的。

    看起来我们已经完(luo)成(suo)很多事情了,但是还不够,我们目前还没有处理子路由,即我们上文路由配置中的children属性,所以我们还需要遍历路由表以便获取到每一个页面对应的路由信息:

    ngOnInit() {
     this.router.events
     .filter(event => event instanceof NavigationEnd)
     .map(() => this.activatedRoute)
     .map((route) => {
     while(route.firstChild) {
     route = router.firstChild;
     }
     return route;
     })
     .subscribe((event) => {
     console.log('NavigationEnd:', event);
     });
    }
    

    最后,我们还需要获取到我们在路由表中为每个路由传入的data信息,然后再利用Title Service设置页面标题:

    ngOnInit() {
     this.router.events
     .filter(event => event instanceof NavigationEnd)
     .map(() => this.activatedRoute)
     .map(route => {
     while (route.firstChild) route = route.firstChild;
     return route;
     })
     .mergeMap(route => route.data)
     .subscribe((event) => this.titleService.setTitle(event['title']));
    }
    

    下面是完成的最终代码,或者也可以到GitHub上查看完整代码:

    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({...})
    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 路由指导

    Angualr2 ActivatedRoute文档

    文档

    Angular 2 利用Router事件和Title实现动态页面标题的方法

    Angular 2 利用Router事件和Title实现动态页面标题的方法:Angular2 为我们提供了名为Title的Service用于修改和获取页面标题,但是如果只是能够在每个页面的ngOnInit方法中为每个页面设置标题岂不是太low了,不符合Angular2高(zhuang)大(bi)的身影。我们想要的结果是在页面改变时能够动态地改变页面标题,如此最好的解
    推荐度:
    标签: 动态 router angular
    • 热门焦点

    最新推荐

    猜你喜欢

    热门推荐

    专题
    Top