确认是否安装了 @angular/animations 模块,如果没有安装则需要安装它。
在 app.module.ts 文件中导入 BrowserAnimationsModule 并将其添加到 imports 数组中:
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
// ...
@NgModule({
imports: [
// ...
BrowserAnimationsModule
],
// ...
})
export class AppModule { }
import { Component } from '@angular/core';
import {
trigger,
transition,
style,
animate,
query,
stagger
} from '@angular/animations';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
animations: [
trigger('routeAnimations', [
transition('* <=> *', [
query(':enter, :leave', [
style({
position: 'absoulte',
left: 0,
width: '100%'
})
]),
query(':enter', [
style({left: '-100%'})
]),
query(':leave', animate('300ms ease-out', style({left: '100%'}))),
query(':enter', animate('300ms ease-out', style({left: '0%'})))
])
])
]
})
export class AppComponent {
}
const routes: Routes = [
{ path: '', component: HomeComponent, data: {animation: 'home'} },
{ path: 'about', component: AboutComponent, data: {animation: 'about'} },
{ path: 'contact', component: ContactComponent, data: {animation: 'contact'} }
];