【问题标题】:angular 5 "Function expressions are not supported in decorators"angular 5“装饰器不支持函数表达式”
【发布时间】:2018-06-16 00:56:02
【问题描述】:

我很难用 ng build --prod

问题出在组件 IndexComponent 中:

index.componenent.ts

import { Component, OnInit } from '@angular/core';
import { indexTransition } from './index.animations';

@Component({
  selector: 'app-index',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.scss'],
  animations: [ indexTransition ],
  host: {
    '[@indexTransition]': ''
  }
})

export class IndexComponent implements OnInit {
  constructor() {
  }

  ngOnInit() {
  }
}

如您所见,它调用 index.animations(在同一文件夹中)

index.animations.ts

import { sequence, trigger, stagger, animate, style, group, query as q, transition, keyframes, animateChild } from '@angular/animations';
const query = (s,a,o={optional:true})=>q(s,a,o);

export const indexTransition = trigger('indexTransition', [
  transition(':enter', [
    query('.iw-path', [
      style({'stroke': '#000'}) 
    ], { optional: true }),
    group([
      query('.iw-path--w', [
        style({'stroke-dashoffset': '100'}),
        animate('3.5s cubic-bezier(.1,.15,.18,1)', style({'stroke-dashoffset': '0'})),
      ], { optional: true }),
      query('.iw-path--basic', [
        style({'stroke-dashoffset': '50'}),
        animate('3.5s cubic-bezier(.1,.15,.18,1)', style({'stroke-dashoffset': '0'})),
      ], { optional: true }),
      query('.iw-path', [
          style({'stroke': '#000'}),
          animate('.5s 2s', style({'stroke': '#e01f1f'})),
      ], { optional: true })
    ]),
  ]),
  transition(':leave', [
    group([
      query('.iw-path--w', [
        style({'stroke-dashoffset': '0'}),
        animate('.5s cubic-bezier(.1,.15,.18,1)', style({'stroke-dashoffset': '100'})),
      ], { optional: true }),
      query('.iw-path--basic', [
        style({'stroke-dashoffset': '0'}),
        animate('.5s cubic-bezier(.1,.15,.18,1)', style({'stroke-dashoffset': '50'})),
      ], { optional: true })
    ]),
  ])
]);

当我使用 ng serve 进行测试时效果很好,但是在使用“build --prod”进行编译时出现以下错误

app\index\index.component.ts(8,17) 中的错误:“IndexComponent”模板编译期间出错 'indexTransition' 中的装饰器不支持函数表达式 'indexTransition' 引用 'ɵ0' 'ɵ0' 包含 app\index\index.animations.ts(2,15) 处的错误 考虑将函数表达式更改为导出函数。

我的环境

Angular CLI:1.7.4

节点:8.10.0

操作系统:win32 x64

角度:5.2.11

该错误与this topic 非常相似,但接受的答案对我不起作用(如您所见,我试图将 optional:true 放在所有查询中)。 我的问题是:在这种情况下,我应该如何“将函数表达式更改为导出函数”?还是有更简单的方法来绕过这个错误?抱歉,我对 Angular 还很陌生。

感谢您的帮助,抱歉英语不好,如果我的问题中缺少某些内容,请告诉我。

【问题讨论】:

    标签: angular typescript angular-transitions


    【解决方案1】:

    不完全确定,但这不可行吗?

    在 index.componenent.ts 中

        animations: [
        Animations.indexTransition
        ]
    

    在 index.animations.ts 中

    export const Animations = {
      indexTransition: trigger('indexTransition', [
        transition(':enter', [
          q('.iw-path', [
            style({'stroke': '#000'})
          ], { optional: true }),
          group([
            q('.iw-path--w', [
              style({'stroke-dashoffset': '100'}),
              animate('3.5s cubic-bezier(.1,.15,.18,1)', style({'stroke-dashoffset': '0'})),
            ], { optional: true }),
            q('.iw-path--basic', [
              style({'stroke-dashoffset': '50'}),
              animate('3.5s cubic-bezier(.1,.15,.18,1)', style({'stroke-dashoffset': '0'})),
            ], { optional: true }),
            q('.iw-path', [
              style({'stroke': '#000'}),
              animate('.5s 2s', style({'stroke': '#e01f1f'})),
            ], { optional: true })
          ]),
        ]),
        transition(':leave', [
          group([
            q('.iw-path--w', [
              style({'stroke-dashoffset': '0'}),
              animate('.5s cubic-bezier(.1,.15,.18,1)', style({'stroke-dashoffset': '100'})),
            ], { optional: true }),
            q('.iw-path--basic', [
              style({'stroke-dashoffset': '0'}),
              animate('.5s cubic-bezier(.1,.15,.18,1)', style({'stroke-dashoffset': '50'})),
            ], { optional: true })
          ]),
        ])
      ])
    };
    

    【讨论】:

    • 我试过你的方法,现在它在我的浏览器控制台中返回“Uncaught TypeError: this.query is not a function”,指向我的 index.animations.ts 文件。
    • 抱歉,这里有一些“这个”。之前缺少一些查询。最后两个(在 :leave 内)将那些 'query' 更改为 'this.query'。
    • 我注意到并添加了它们,但仍然与上面的结果相同
    • 修改了很多代码。现在应该工作得更好了。
    • 这似乎工作......我不敢相信我是第一个遇到这个问题的人。非常感谢!
    猜你喜欢
    • 2018-07-26
    • 2021-03-17
    • 1970-01-01
    • 2019-04-08
    • 2020-09-25
    • 2018-06-11
    • 1970-01-01
    • 2018-12-30
    相关资源
    最近更新 更多