在库模块中定义APP_INITIALIZER
时,构建失败并出现Lambda not supported
错误。根据docs导出函数时会引发构建错误:
import { NgModule, APP_INITIALIZER } from '@angular/core';
import { MylibComponent } from './mylib.component';
export function myLibInit() {
return () => console.log('Hi from exported function');
}
@NgModule({
providers: [
{
provide: APP_INITIALIZER,
multi: true,
useFactory: myLibInit
}
]
})
export class MylibModule { }
dev
在和中都会引发构建错误prod
。
我也尝试过使用 ES6 对象方法简写符号定义工厂函数:
import { NgModule, APP_INITIALIZER } from '@angular/core';
import { MylibComponent } from './mylib.component';
@NgModule({
providers: [
{
provide: APP_INITIALIZER,
multi: true,
useFactory() {
return () => console.log('Hi from ES6 object method shorthand')
}
}
]
})
export class MylibModule { }
这同时通过了dev
和构建,但是当使用标志构建库和应用程序时,prod
应用程序会在运行时引发错误。ERROR TypeError: this.appInits[r] is not a function
prod
一个人如何APP_INITIALIZER
在库中正确使用而不出现构建或运行时错误?
可以在这里找到复制品:
git clone https://github.com/samherrmann/angular-sandbox.git
cd angular-sandbox
git checkout lambda-not-supported
npm install
npm run build
可以在此处找到有关此问题的 GitHub 问题。