欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

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

10年積累的成都網(wǎng)站制作、做網(wǎng)站經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有海勃灣免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

首先在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í)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

新聞標(biāo)題:angular4如何在全局設(shè)置路由跳轉(zhuǎn)動畫的方法
當(dāng)前鏈接:http://chinadenli.net/article44/gjdjhe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化商城網(wǎng)站、網(wǎng)站內(nèi)鏈網(wǎng)站建設(shè)、做網(wǎng)站服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)