angular4 如何在全局設(shè)置路由跳轉(zhuǎn)動畫的方法
最近用angular4寫項(xiàng)目需要為每次路由跳轉(zhuǎn)增加動畫,看了一下官方文檔,雖然可以實(shí)現(xiàn),但是要每個(gè)組件都引入一次animations,比較麻煩,找網(wǎng)上也查閱了很多資料,但是都沒找到適用的方法,最后自己寫了一種方法如下:
首先在app.module中導(dǎo)入BrowserAnimationsModule
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
@NgModule({
imports: [
BrowserAnimationsModule
在根目錄src/app/下創(chuàng)建一個(gè)animations.ts。內(nèi)容如下,這里我用到query和group是想兩個(gè)頁面來回切換有過度痕跡
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)都有動畫了,不用一個(gè)一個(gè)組件導(dǎo)入animations。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入理解Angularjs中$http.post與$.post
本篇文章主要介紹了深入理解Angularjs中$http.post與$.post ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
AngularJs定時(shí)器$interval 和 $timeout詳解
這篇文章主要介紹了AngularJs定時(shí)器$interval 和 $timeout詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
Angular實(shí)現(xiàn)響應(yīng)式表單
本篇文章主要介紹了Angular實(shí)現(xiàn)響應(yīng)式表單,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
angular學(xué)習(xí)之動態(tài)創(chuàng)建表單的方法
這篇文章主要介紹了angular學(xué)習(xí)之動態(tài)創(chuàng)建表單的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
AngularJS 中的指令實(shí)踐開發(fā)指南(一)
指令(Directives)是所有AngularJS應(yīng)用最重要的部分。盡管AngularJS已經(jīng)提供了非常豐富的指令,但還是經(jīng)常需要創(chuàng)建應(yīng)用特定的指令。這篇教程會為你講述如何自定義指令,以及介紹如何在實(shí)際項(xiàng)目中使用2016-03-03
AngularJS服務(wù)service用法總結(jié)
這篇文章主要介紹了AngularJS服務(wù)service用法,結(jié)合實(shí)例形式總結(jié)分析了服務(wù)Service的概念、功能及自定義服務(wù)的相關(guān)操作技巧,需要的朋友可以參考下2016-12-12
Angularjs根據(jù)json文件動態(tài)生成路由狀態(tài)的實(shí)現(xiàn)方法
最近做項(xiàng)目遇到這樣一個(gè)新需求,就是需要根據(jù)json文件動態(tài)生成路由狀態(tài),通過查閱相關(guān)資料實(shí)現(xiàn)了此功能,下面小編把問題總結(jié)分享到腳本之家平臺供大家參考2017-04-04

