angular: APP_INITIALIZER with useFactory Compilation Error on first run
I’m submitting a … (check one with “x”)
[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
Current behavior When using APP_INITIALIZER with useFactory I have an error on first compilation run, on the second run the error goes away.
Expected behavior No errors on first compilation run.
Minimal reproduction of the problem with instructions
@NgModule({
providers: [
{
provide: APP_INITIALIZER,
useFactory: fooFactory,
deps: [MainConfigService],
multi: true
},
],
...
export function foo(mainConfigService) {
return function() {
return mainConfigService.loadConf();
};
};
export function fooFactory(mainConfigService: MainConfigService) {
return foo(mainConfigService);
}
What is the motivation / use case for changing the behavior? We need a working demo/way to asynchronously bootstap the application.
Please tell us about your environment: The issue has been replicated on Windows 10 and Ubuntu 16.04.1 LTS
-
Angular version: 2.0.X angular-cli: 1.0.0-beta.28.3 node: 7.0.0 os: linux x64 @angular/common: 2.4.7 @angular/compiler: 2.4.7 @angular/core: 2.4.7 @angular/forms: 2.4.7 @angular/http: 2.4.7 @angular/platform-browser: 2.4.7 @angular/platform-browser-dynamic: 2.4.7 @angular/router: 3.4.7 @angular/compiler-cli: 2.4.7 @angular/cli: 1.0.0-beta.31
-
Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ] Chrome XX
-
Language: [all | TypeScript X.X | ES6/7 | ES5] TypeScript X.X
-
Node (for AoT issues):
node --version
=
none
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 16 (9 by maintainers)
@mrucelum … or use
// @dynamic
comment … just put it exactly before the line with@NgModule
.And then you can use:
I have got down to the root of my problem, The APP_INITIALIZER was actually a red herring the real issue was with the way I was declaring the ngrx Store
Plus after that I had to remove all the
default
of the modules declared withexport default
. After that the problem was solved. Unfortunately I had to result to dichotomy to find what was the issue. The Error was completely misleading. Thank you for the support though.try to change
to
@cmckni3 … you can’t use an arrow function in that place. Use exported factory function like:
where
appConfigServiceFactory
is something like:I’ve just tested this:
and it works just fine. 2.0.10 ts 2.4.6 angular
Yes, I had an exported function and switched to arrow function which broke it. Switched back to fix it.