角度 6 AOT 问题 - 无法将函数作为参数传递



当尝试使用ng-build--prod进行构建时,我无法将函数作为参数从子类传递给基类。在没有--prod标志的情况下,构建可以很好地工作,这似乎表明AOT有问题。我得到的错误是:

中的错误:无法解析中AppGridComponent的所有参数/src/app/components/core/shared/app-grid.component.ts:(?,[object对象](

我找到了这个SO线程,它有几个不同的解决方案答案,但我还没能找到任何答案。AOT似乎想将此参数作为服务注入,但无法解析(我不需要,因为我将函数作为子函数的值传递(。

基类-app-grid.component.ts

import { Component, OnDestroy, OnInit } from '@angular/core';
import { GlobalsService } from '../../../services/globals.service';
@Component({})
export class AppGridComponent implements OnInit, OnDestroy {
constructor(protected loadDataCallBack: any, protected globalsService: GlobalsService) {
}
ngOnInit() {
this.init();
}
// get api data
public init() {
this.loadDataCallBack()
.subscribe(result => result);
}

子类-rules.component.ts

const loadApiData = function() {
return this.productRuleService.get();
};
@Component({
selector: 'app-rules',
template: `<div class="grid-wrapper">Data here...</div>`
})
export class RulesComponent extends AppGridComponent implements OnInit, OnDestroy {
constructor(protected globalsService: GlobalsService, protected productRuleService: ProductRelationshipRuleService) {
super(loadApiData, globalsService);
}

任何关于如何构建的建议都将不胜感激。

我创建了一个扩展Function的类,然后将该类用作组件中的提供程序,从而实现了这一点。

// Base Class - app-grid.component.ts
export class LoadDataCallBack extends Function {
}
@Component({
template: '',
providers: [{provide: LoadDataCallBack, useValue: () => {}}]
})

这最终满足了编译器的要求,并且它能够识别要注入到第一个参数中的类型:loadDataCallBack。

最新更新