javascript - 为什么 Angular 5 Transition 抛出 AppComponent.html :2 ERROR TypeError: Cannot read property 'forEach' of undefined

标签 javascript angular typescript animation

为什么 Angular 5 会抛出这个错误?

AppComponent.html:2 ERROR TypeError: Cannot read property 'forEach' of undefined

我正在研究 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() {
  }

}

错误引用的代码行只是包含我的组件的标记:<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)

我的 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 { }

我正在尝试遵循 angular site 上的代码尽我所能。有人可以告诉我我忘记了什么吗?有没有我忘记的进口? 有我需要下载的包吗?我是 Angular 和 Angular 动画的新手,我似乎无法弄清楚这一点。

最佳答案

如果你仔细看教程,动画的“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
]

关于javascript - 为什么 Angular 5 Transition 抛出 AppComponent.html :2 ERROR TypeError: Cannot read property 'forEach' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48214511/

相关文章:

javascript - 自动化应用程序测试 (phantomjs) 和 merge 分支

JavaScript 示例,阐明函数内函数的语言模式设计

javascript - Bootstrap Validator Summernote 字段不会重新验证

javascript - 不确定为什么 Angular 4 中的数据无法从子组件正确传递到父组件

javascript - Angular + http服务 : POST & DELETE results?

arrays - typescript :数组类型取决于先前的数组元素

javascript - 如何隐藏附加的最后一个框?

javascript - 单击 Angular 中的编辑内联编辑表行

angular - ng2-翻译使用本地存储

angular - 错误: p-button (and p-password) is not a known element