为什么Angular 5 Transition抛出AppComponent.html:2 ERROR TypeError:无法读取未定义的属性'forEach'

Kni*_*ght 8 javascript animation typescript angular

为什么Angular 5会抛出此错误?

AppComponent.html:2 ERROR TypeError: Cannot read property 'forEach' of undefined
Run Code Online (Sandbox Code Playgroud)

我正在研究Angular动画的概念验证,我正在使用该站点的代码.我的组件看起来像这样:

import { Component, OnInit } from '@angular/core';
import { trigger, state, style, transition, animate, keyframes } from '@angular/animations';

@Component({
  selector: 'app-obj-list',
  templateUrl: './obj-list.component.html',
  styleUrls: ['./obj-list.component.css'],
    animations: [

    trigger('myAwesomeAnimation', [
        state('small', style({
            transform: 'scale(1)',
        })),
        state('large', style({
            transform: 'scale(1.2)',
        })),
    ]),
    transition('small => large', animate('100ms ease-in'))

    ]
})
export class ObjListComponent implements OnInit {
  state = 'small';
  constructor() { }
  animateMe() {
     this.state = (this.state === 'small' ? 'large' : 'small');
       }

  ngOnInit() {
  }

}
Run Code Online (Sandbox Code Playgroud)

错误引用的代码行只是包含我的组件的标记: <app-obj-list></app-obj-list>

我把它缩小到上面文件中的行: transition('small => large', animate('100ms ease-in'))

当我删除该行时,错误就消失了.当我离开时,我收到错误:

AppComponent.html:2 ERROR TypeError: Cannot read property 'forEach' of undefined
    at new AnimationTrigger (browser.js:3017)
    at buildTrigger (browser.js:3005)
    at InjectableAnimationEngine.AnimationEngine.registerTrigger (browser.js:5504)
    at eval (animations.js:291)
    at Array.forEach (<anonymous>)
    at AnimationRendererFactory.createRenderer (animations.js:290)
    at DebugRendererFactory2.createRenderer (core.js:15075)
    at createComponentView (core.js:13633)
    at callWithDebugContext (core.js:15041)
    at Object.debugCreateComponentView [as createComponentView] (core.js:14370)
Run Code Online (Sandbox Code Playgroud)

我的app.module有BrowserAnimationsModule,所以不应该是问题:

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';


import { AppComponent } from './app.component';
import { ObjListComponent } from './obj-list/obj-list.component';


@NgModule({
  declarations: [
    AppComponent,
    ObjListComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

我正试图尽可能地跟踪角度网站上的代码.谁能告诉我我忘了什么?我忘记了进口吗?我需要下载一个包吗?我是Angular和Angular动画的新手,我似乎无法弄清楚这一点.

Jul*_*bos 12

如果仔细观察教程,动画的"transition"属性需要在"trigger"数组中:

animations: [
    trigger('myAwesomeAnimation', [
        state('small', style({
            transform: 'scale(1)',
        })),
        state('large', style({
            transform: 'scale(1.2)',
        })), 
        transition('small => large', animate('100ms ease-in'))
    ]) // close the array after the transition
]
Run Code Online (Sandbox Code Playgroud)

  • 哎呀 有时候这是最小的事情,使我们绊倒!非常感谢!我无法在8分钟内接受您的回答,但我会尽快回复。你摇滚! (2认同)