最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Angular 2 利用Router事件和Title實(shí)現(xiàn)動(dòng)態(tài)頁(yè)面標(biāo)題的方法

 更新時(shí)間:2017年08月23日 16:52:26   作者:劉文壯  
本篇文章主要介紹了Angular 2 利用Router事件和Title實(shí)現(xiàn)動(dòng)態(tài)頁(yè)面標(biāo)題的方法,具有一定的參考價(jià)值,有興趣的可以了解一下

Angular2 為我們提供了名為T(mén)itle的Service用于修改和獲取頁(yè)面標(biāo)題,但是如果只是能夠在每個(gè)頁(yè)面的ngOnInit方法中為每個(gè)頁(yè)面設(shè)置標(biāo)題豈不是太low了,不符合Angular2高(zhuang)大(bi)的身影。我們想要的結(jié)果是在頁(yè)面改變時(shí)能夠動(dòng)態(tài)地改變頁(yè)面標(biāo)題,如此最好的解決方案就是組合使用Router事件和Title Service。

Title Service

使用Service自然首先要將其引入,不過(guò)要注意Title Service并不在@angular/core中,而是在@angular/platform-browser中:

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

引入之后,自然要將其注入到當(dāng)前組件中,而這通常利用constructor完成:

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

很顯然,Title Service應(yīng)該有某些操作頁(yè)面標(biāo)題的方法,不管通過(guò)查找文檔還是查找源碼我們都能很容易知道其只有兩個(gè)方法:

  • getTitle() 用于獲取當(dāng)前當(dāng)前頁(yè)面的標(biāo)題
  • setTitle(newTitle: String) 用于設(shè)置當(dāng)前頁(yè)面的標(biāo)題

如果只是簡(jiǎn)單地靜態(tài)地設(shè)置頁(yè)面標(biāo)題,則可以在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方法設(shè)置文檔標(biāo)題是較好的時(shí)機(jī),當(dāng)然也可以根據(jù)自己的需求在任意地方使用setTitle方法。

Router和Router事件

使用Router和使用Title Service流程基本一致,先引入后注入,不過(guò)要注意Router和Title Service類(lèi)似并不位于@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中通過(guò)URL、Router和Component之間的對(duì)應(yīng)關(guān)系進(jìn)行頁(yè)面之間的跳轉(zhuǎn),Router把瀏覽器中的URL看做一個(gè)操作指南,據(jù)此可導(dǎo)航到一個(gè)由客戶(hù)端生成的視圖,并可以把參數(shù)傳給支撐視圖的相應(yīng)組件。所以我們需要定義路由表:

// 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'} }
];
 

注意路徑和組件之間的對(duì)應(yīng)關(guān)系,并且為了能夠在Router事件中獲取到頁(yè)面標(biāo)題,我們?cè)诼酚杀碇校瑸橐恍╉?yè)面提供了數(shù)據(jù)data,并在data中設(shè)置了表示頁(yè)面標(biāo)題的title屬性。

Router事件

利用Router事件我們就可以實(shí)現(xiàn)動(dòng)態(tài)改變頁(yè)面標(biāo)題的目的,不過(guò)放置的位置很重要,我們這里選擇在A(yíng)ppComponent的ngOnInit方法中利用subscribe訂閱Router事件,因?yàn)锳ppComponent是根組件,所以能夠訂閱所有Router事件:

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

當(dāng)然我們這里這對(duì)NavigationEnd事件感興趣:

import {ActivatedRoute} from '@angular/router';
// import bala...

// other codes

ngOnInit() {
 this.router.events
  .subscribe((event) => {
   if (event instanceof NavigationEnd) {
    console.log('NavigationEnd:', event);
   }
  });
}

當(dāng)然使用這種判斷篩選的方式并沒(méi)有錯(cuò),但是在現(xiàn)在的前端世界里顯得不夠優(yōu)雅,我們應(yīng)該使用RxJS中的filter達(dá)到我們的目的:

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);
 });
}

當(dāng)然,我們?nèi)绻胍獎(jiǎng)討B(tài)改變某個(gè)頁(yè)面的標(biāo)題,就需要獲取到當(dāng)前被展示的頁(yè)面對(duì)應(yīng)的路由信息,而這可以通過(guò)ActivatedRoute得到,其使用方式和Title Service及Router類(lèi)似,不再贅述:

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來(lái)更優(yōu)雅地達(dá)成我們目的。

看起來(lái)我們已經(jīng)完(luo)成(suo)很多事情了,但是還不夠,我們目前還沒(méi)有處理子路由,即我們上文路由配置中的children屬性,所以我們還需要遍歷路由表以便獲取到每一個(gè)頁(yè)面對(duì)應(yīng)的路由信息:

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);
 });
}

最后,我們還需要獲取到我們?cè)诼酚杀碇袨槊總€(gè)路由傳入的data信息,然后再利用Title Service設(shè)置頁(yè)面標(biāo)題:

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 路由指導(dǎo)

Angualr2 ActivatedRoute文檔

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于A(yíng)ngularJS拖拽插件ngDraggable.js實(shí)現(xiàn)拖拽排序功能

    基于A(yíng)ngularJS拖拽插件ngDraggable.js實(shí)現(xiàn)拖拽排序功能

    ngDraggable.js是一款比較簡(jiǎn)單實(shí)用的angularJS拖拽插件,借助于封裝好的一些自定義指令,能夠快速的進(jìn)行一些拖拽應(yīng)用開(kāi)發(fā)。本文先從基本概念入手,給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2019-04-04
  • AngularJS 過(guò)濾與排序詳解及實(shí)例代碼

    AngularJS 過(guò)濾與排序詳解及實(shí)例代碼

    這篇文章主要介紹了AngularJS 過(guò)濾與排序,這里整理了詳細(xì)的資料及簡(jiǎn)單實(shí)例代碼,有需要的小伙伴可以參考下
    2016-09-09
  • Agularjs妙用雙向數(shù)據(jù)綁定實(shí)現(xiàn)手風(fēng)琴效果

    Agularjs妙用雙向數(shù)據(jù)綁定實(shí)現(xiàn)手風(fēng)琴效果

    最近在工作總遇到需要實(shí)現(xiàn)類(lèi)似手風(fēng)琴效果的需求,下面小編通過(guò)本文給大家分享angularjs巧用雙向數(shù)據(jù)綁定實(shí)現(xiàn)手風(fēng)琴效果,需要的朋友可以參考下
    2017-05-05
  • AngularJS入門(mén)教程二:在路由中傳遞參數(shù)的方法分析

    AngularJS入門(mén)教程二:在路由中傳遞參數(shù)的方法分析

    這篇文章主要介紹了AngularJS在路由中傳遞參數(shù)的方法,結(jié)合實(shí)例形式分析了AngularJS實(shí)現(xiàn)路由中傳遞參數(shù)的相關(guān)技巧,并總結(jié)了相關(guān)操作步驟與注意事項(xiàng),需要的朋友可以參考下
    2017-05-05
  • angular.js中解決跨域問(wèn)題的三種方式

    angular.js中解決跨域問(wèn)題的三種方式

    跨域,前端開(kāi)發(fā)中經(jīng)常遇到的問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于angular.js中解決跨域問(wèn)題的三種方式,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07
  • angular ng-repeat數(shù)組中的數(shù)組實(shí)例

    angular ng-repeat數(shù)組中的數(shù)組實(shí)例

    下面小編就為大家?guī)?lái)一篇angular ng-repeat數(shù)組中的數(shù)組實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • 詳解angular中通過(guò)$location獲取路徑(參數(shù))的寫(xiě)法

    詳解angular中通過(guò)$location獲取路徑(參數(shù))的寫(xiě)法

    本篇文章主要介紹了詳解angular中通過(guò)$location獲取路徑(參數(shù))的寫(xiě)法 ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Angular服務(wù)Request異步請(qǐng)求的實(shí)例講解

    Angular服務(wù)Request異步請(qǐng)求的實(shí)例講解

    今天小編就為大家分享一篇Angular服務(wù)Request異步請(qǐng)求的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • angular.js指令中transclude選項(xiàng)及ng-transclude指令詳解

    angular.js指令中transclude選項(xiàng)及ng-transclude指令詳解

    這篇文章主要研究一下如何使用ng-transclude指令,以及指令的transclude選項(xiàng)的相關(guān)資料,文中介紹的非常詳細(xì),并給出了完整的示例代碼大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。
    2017-05-05
  • Angular4 中內(nèi)置指令的基本用法

    Angular4 中內(nèi)置指令的基本用法

    不得不說(shuō)指令是ng最為強(qiáng)大的功能之一,好吧,也可以去掉之一,是最強(qiáng)大的功能。下面這篇文章主要給大家介紹了關(guān)于A(yíng)ngular4中內(nèi)置指令的基本用法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面來(lái)一起看看吧。
    2017-07-07

最新評(píng)論

萨嘎县| 页游| 沙河市| 家居| 吉林省| 新津县| 天柱县| 弋阳县| 苍南县| 沙雅县| 凤凰县| 鄂伦春自治旗| 阿城市| 荣昌县| 施秉县| 白朗县| 紫金县| 汶川县| 莎车县| 宁蒗| 奎屯市| 三明市| 长垣县| 营口市| 连城县| 正安县| 青川县| 罗山县| 宜昌市| 通许县| 闸北区| 普安县| 荥阳市| 霍林郭勒市| 甘谷县| 嘉祥县| 开鲁县| 北辰区| 邵东县| 林周县| 深水埗区|