Angular 5动画咨询:入门学习官方「进入与离开」指南遇问题
Hey there! Since you're new to Angular animations and working through the official "Enter and Leave" guide, let's break down the common issues that might be tripping you up with your flyInOut animation setup.
First, let's cover the gaps in your current code and fix them step by step:
1. Complete Your Animation Imports
It looks like your import statement was cut off — you need to include transition (the full animation API method) to define how your animation moves between states:
import { Component, OnInit } from '@angular/core'; import { SiteService } from '../../Services/site.service'; import { Site } from './site'; // Full animation imports needed for enter/leave logic import { trigger, state, style, animate, transition } from '@angular/animations';
2. Define the flyInOut Trigger Properly
You’ve referenced [@flyInOut]="site.state" in your template, but you need to define this trigger in your component’s animations metadata. Here’s a complete example that handles both enter (when a site is added to the list) and leave (when it’s removed) animations:
@Component({ selector: 'app-site-list', // Update this to match your component's selector templateUrl: './site-list.component.html', styleUrls: ['./site-list.component.css'], animations: [ trigger('flyInOut', [ // Define the "active" state for visible items state('in', style({ opacity: 1, transform: 'translateX(0)' })), // Animation for when an item enters the DOM (void => *) transition('void => *', [ style({ opacity: 0, transform: 'translateX(-100%)' }), animate(300) // 300ms duration for the enter animation ]), // Animation for when an item leaves the DOM (* => void) transition('* => void', [ animate(300, style({ opacity: 0, transform: 'translateX(100%)' })) ]) ]) ] }) export class SiteListComponent implements OnInit { sites: Site[] = []; constructor(private siteService: SiteService) { } ngOnInit(): void { // Fetch your sites here, and make sure each site has a `state` property initialized to 'in' this.siteService.getSites().subscribe(sites => { this.sites = sites.map(site => ({ ...site, state: 'in' })); }); } }
3. Ensure BrowserAnimationsModule Is Imported
Angular animations won’t work at all unless you import BrowserAnimationsModule in your root module (usually AppModule):
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; @NgModule({ imports: [ // ... your other imports (BrowserModule, HttpClientModule, etc.) BrowserAnimationsModule ], // ... }) export class AppModule { }
Common Issues to Check
- Animation not firing? Double-check that the trigger name in your template (
flyInOut) matches exactly with the trigger name defined in your component. Also, confirm that eachsiteobject has astateproperty set to'in'when it’s added to the list. - Syntax errors? Make sure all animation methods (like
transition,animate) are properly closed with parentheses, and that your style objects have valid CSS properties. - No leave animation when deleting items? If you’re removing items from the
sitesarray, Angular will automatically trigger the* => voidtransition — just make sure you’re not bypassing Angular’s change detection (e.g., using direct DOM manipulation instead of updating the array).
If you’re hitting a specific error or the animation still isn’t behaving as expected, feel free to share the full error message or the rest of your component code, and we’ll dig deeper!
内容的提问来源于stack exchange,提问作者Sergey




