亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

angular4 如何在全局設(shè)置路由跳轉(zhuǎn)動畫的方法

 更新時間:2017年08月30日 09:57:39   作者:陳俊翰  
本篇文章主要介紹了angular4 如何在全局設(shè)置路由跳轉(zhuǎn)動畫的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近用angular4寫項目需要為每次路由跳轉(zhuǎn)增加動畫,看了一下官方文檔,雖然可以實現(xiàn),但是要每個組件都引入一次animations,比較麻煩,找網(wǎng)上也查閱了很多資料,但是都沒找到適用的方法,最后自己寫了一種方法如下:

首先在app.module中導(dǎo)入BrowserAnimationsModule

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

@NgModule({
 imports: [
  BrowserAnimationsModule

在根目錄src/app/下創(chuàng)建一個animations.ts。內(nèi)容如下,這里我用到query和group是想兩個頁面來回切換有過度痕跡

import { AnimationEntryMetadata, state } from '@angular/core';
import { trigger, transition, animate, style, query, group } from '\@angular/animations';

export const routeAnimation: AnimationEntryMetadata =
 trigger('routeAnimation', [
  transition(':enter', [
   style({
    position: 'absolute'
   }),
   animate('0.5s ease-in-out')
  ]),
  transition('* => *', [
   query(':leave', style({ transform: 'translateX(0)', position: 'absolute'}), { optional: true }),
   query(':enter', style({ transform: 'translateX(100%)', position: 'absolute'}), { optional: true }),

   group([
    query(':leave', animate('.5s ease-in-out', style({transform: 'translateX(-100%)'})), { optional: true }),
    query(':enter', animate('.5s ease-in-out', style({transform: 'translateX(0)'})), { optional: true })
   ])
  ])
 ]);

接著在app.component中使用 NavigationEnd 設(shè)置每次路由跳轉(zhuǎn)監(jiān)聽的參數(shù)變化并且引入animations模塊

import { Router, NavigationEnd } from '@angular/router';
import { routeAnimation } from './animations';

@Component({
 selector: 'app-root',
 templateUrl: './app.component.html',
 styleUrls: ['./app.component.less'],
 animations: [routeAnimation]
})
// router跳轉(zhuǎn)動畫所需參數(shù)
 routerState: boolean = true;
 routerStateCode: string = 'active';

this.router.events.subscribe(event => {
   if (event instanceof NavigationEnd) {
    // 每次路由跳轉(zhuǎn)改變狀態(tài)
   this.routerState = !this.routerState;
   this.routerStateCode = this.routerState ? 'active' : 'inactive';
   }
  });

最后在app.component.html中聲明路由動畫就可以了

<div id="app" [@routeAnimation]="routerStateCode">
 <router-outlet></router-outlet>
</div>

現(xiàn)在全局路由跳轉(zhuǎn)都有動畫了,不用一個一個組件導(dǎo)入animations。

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

相關(guān)文章

  • 淺談AngularJs 雙向綁定原理(數(shù)據(jù)綁定機制)

    淺談AngularJs 雙向綁定原理(數(shù)據(jù)綁定機制)

    本篇文章主要介紹了淺談AngularJs 雙向綁定原理(數(shù)據(jù)綁定機制),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • Angularjs 與 bower安裝和使用詳解

    Angularjs 與 bower安裝和使用詳解

    這篇文章主要介紹了Angularjs 與 bower安裝和使用詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 利用Jasmine對Angular進行單元測試的方法詳解

    利用Jasmine對Angular進行單元測試的方法詳解

    單元測試是一種能夠幫助開發(fā)者驗證代碼中某一部分有效性的技術(shù)。下面這篇文章主要給大家介紹了關(guān)于利用Jasmine對Angular進行單元測試的相關(guān)資料,文中介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-06-06
  • Angular之jwt令牌身份驗證的實現(xiàn)

    Angular之jwt令牌身份驗證的實現(xiàn)

    這篇文章主要介紹了Angular之jwt令牌身份驗證的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • 詳解webpack+angular2開發(fā)環(huán)境搭建

    詳解webpack+angular2開發(fā)環(huán)境搭建

    這篇文章主要介紹了詳解webpack+angular2開發(fā)環(huán)境搭建,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • AngularJS讀取JSON及XML文件的方法示例

    AngularJS讀取JSON及XML文件的方法示例

    這篇文章主要介紹了AngularJS讀取JSON及XML文件的方法,涉及AngularJS針對xml及json格式文件數(shù)據(jù)的讀取、遍歷、輸出等相關(guān)操作技巧,需要的朋友可以參考下
    2017-05-05
  • 在 Angular6 中使用 HTTP 請求服務(wù)端數(shù)據(jù)的步驟詳解

    在 Angular6 中使用 HTTP 請求服務(wù)端數(shù)據(jù)的步驟詳解

    本文分步驟給大家介紹了在 Angular6 中使用 HTTP 請求服務(wù)端數(shù)據(jù)的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-08-08
  • Angular在一個頁面中使用兩個ng-app的方法

    Angular在一個頁面中使用兩個ng-app的方法

    這篇文章主要介紹了Angular在一個頁面中使用兩個ng-app的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-02-02
  • Facade Service暴露commands簡化代碼邏輯提高可訪問性組合性

    Facade Service暴露commands簡化代碼邏輯提高可訪問性組合性

    在 Angular 應(yīng)用開發(fā)中,使用 Facade Service 暴露 commands(命令)以及訂閱這些 commands 是一個常見的設(shè)計模式,本文將詳細介紹在 Facade Service 中如何實現(xiàn)這一目標,并深入探討相關(guān)細節(jié),以及通過實際示例進行說明
    2023-10-10
  • 詳解Angular Forms中自定義ngModel綁定值的方式

    詳解Angular Forms中自定義ngModel綁定值的方式

    在Angular應(yīng)用中有兩種方式來實現(xiàn)表單綁定,但是對于一些特殊的表單控件沒法實現(xiàn),這篇文章主要介紹了詳解Angular Forms中自定義ngModel綁定值的方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12

最新評論