国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

angular4 如何在全局設置路由跳轉動畫的方法

2024-05-06 15:12:07
字體:
來源:轉載
供稿:網友

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

首先在app.module中導入BrowserAnimationsModule

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';@NgModule({ imports: [  BrowserAnimationsModule

在根目錄src/app/下創建一個animations.ts。內容如下,這里我用到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 設置每次路由跳轉監聽的參數變化并且引入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跳轉動畫所需參數 routerState: boolean = true; routerStateCode: string = 'active';this.router.events.subscribe(event => {   if (event instanceof NavigationEnd) {    // 每次路由跳轉改變狀態   this.routerState = !this.routerState;   this.routerStateCode = this.routerState ? 'active' : 'inactive';   }  });

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

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

現在全局路由跳轉都有動畫了,不用一個一個組件導入animations。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 唐河县| 长寿区| 连江县| 巧家县| 阳山县| 东方市| 常熟市| 凤翔县| 曲阳县| 北辰区| 嘉义市| 安龙县| 高陵县| 运城市| 夹江县| 九寨沟县| 庆城县| 光山县| 平安县| 泉州市| 苗栗市| 嵊州市| 东兴市| 通海县| 布尔津县| 逊克县| 海宁市| 江达县| 昭通市| 南康市| 婺源县| 北票市| 天镇县| 寿光市| 务川| 昌宁县| 蕉岭县| 咸宁市| 鲁山县| 武宁县| 台东市|