如何实现角路线动画?

问题描述 投票:-1回答:1

我正在尝试实现角度路线动画。我已经按照文档以及以下视频进行了介绍:https://www.youtube.com/watch?v=7JA90VI9fAI。但是我无法使动画正常工作。这是到目前为止的代码:https://stackblitz.com/edit/angular-animations-not-working

animations.ts

import { trigger, transition, animate, style, query } from '@angular/animations';

export const fader =
  trigger('routeAnimations', [
    transition('* <=> *', [
      query(':enter, :leave', [
        style({
          position: 'absolute',
          left: 0,
          width: '100%',
          opacity: 0,
          transform: 'scale(0) translateY(100%)'
        })
      ], { optional: true }),
      query(':enter', [
        animate('600ms ease', style({
          opacity: 1, transform: 'scale(1) translateY(0)'
        })),
      ], { optional: true })
    ])
  ]);

app.component.html

<a [routerLink]="['home']">Home</a>&nbsp;&nbsp;&nbsp;<a [routerLink]="['hello']">Hello</a>
<div class="route-content" [@routeAnimations]="prepareRoute(outlet)">
  <router-outlet #outlet="outlet"></router-outlet>
</div>

app.component.ts

import { Component } from '@angular/core';
import { fader } from './animations';
import { RouterOutlet } from '@angular/router';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ],
  animations: [
    fader
  ]
})
export class AppComponent  {
  name = 'Angular';
  prepareRoute(outlet: RouterOutlet) {
    return outlet && outlet.activatedRouteData && outlet.activatedRouteData['animation'];
  }
}

styles.css

.route-content {
  position: relative;
}

有人可以帮忙吗?

提前感谢。

angular angular-animations
1个回答
1
投票

您需要在路线中添加数据:{animation:'HomePage'}

  { path: 'home', component: HomeComponent, data: {animation: 'HomePage'} },

编码为https://angular.io/guide/route-animations

© www.soinside.com 2019 - 2024. All rights reserved.