1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      Angular2利用Router事件和Title實現(xiàn)動態(tài)頁面標題的方法

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

      成都創(chuàng)新互聯(lián)自2013年起,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都做網(wǎng)站、成都網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元欒城做網(wǎng)站,已為上家服務(wù),為欒城各地企業(yè)和個人服務(wù),聯(lián)系電話:18980820575

      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應(yīng)該有某些操作頁面標題的方法,不管通過查找文檔還是查找源碼我們都能很容易知道其只有兩個方法:

      • getTitle() 用于獲取當前當前頁面的標題
      • setTitle(newTitle: String) 用于設(shè)置當前頁面的標題

      如果只是簡單地靜態(tài)地設(shè)置頁面標題,則可以在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è)置文檔標題是較好的時機,當然也可以根據(jù)自己的需求在任意地方使用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之間的對應(yīng)關(guān)系進行頁面之間的跳轉(zhuǎn),Router把瀏覽器中的URL看做一個操作指南,據(jù)此可導(dǎo)航到一個由客戶端生成的視圖,并可以把參數(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'} }
      ];
       

      注意路徑和組件之間的對應(yīng)關(guān)系,并且為了能夠在Router事件中獲取到頁面標題,我們在路由表中,為一些頁面提供了數(shù)據(jù)data,并在data中設(shè)置了表示頁面標題的title屬性。

      Router事件

      利用Router事件我們就可以實現(xiàn)動態(tài)改變頁面標題的目的,不過放置的位置很重要,我們這里選擇在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);
         }
        });
      }
      
      

      當然使用這種判斷篩選的方式并沒有錯,但是在現(xiàn)在的前端世界里顯得不夠優(yōu)雅,我們應(yīng)該使用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);
       });
      }
      
      

      當然,我們?nèi)绻胍獎討B(tài)改變某個頁面的標題,就需要獲取到當前被展示的頁面對應(yīng)的路由信息,而這可以通過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來更優(yōu)雅地達成我們目的。

      看起來我們已經(jīng)完(luo)成(suo)很多事情了,但是還不夠,我們目前還沒有處理子路由,即我們上文路由配置中的children屬性,所以我們還需要遍歷路由表以便獲取到每一個頁面對應(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);
       });
      }
      

      最后,我們還需要獲取到我們在路由表中為每個路由傳入的data信息,然后再利用Title Service設(shè)置頁面標題:

      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文檔

      以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


      網(wǎng)站名稱:Angular2利用Router事件和Title實現(xiàn)動態(tài)頁面標題的方法
      標題來源:http://www.ef60e0e.cn/article/igghcd.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        大足县| 交口县| 黄平县| 重庆市| 湖南省| 堆龙德庆县| 本溪市| 茌平县| 郁南县| 吴堡县| 双鸭山市| 汤阴县| 资阳市| 新绛县| 崇仁县| 商水县| 芦溪县| 公安县| 育儿| 龙里县| 福安市| 通辽市| 遂溪县| 红原县| 南郑县| 贵南县| 县级市| 灌云县| 襄汾县| 庐江县| 木里| 安宁市| 中宁县| 宁城县| 天台县| 都匀市| 随州市| 龙川县| 屯门区| 沂源县| 梁山县|