useFactory in @Injectable doesn't work using object's shorthand method definition
See original GitHub issueπ bug report
Affected Package
The issue is caused by package @angular/coreIs this a regression?
Yes, the previous version in which this bug was not present was: I don't knowDescription
useFactory
doesnβt work when I use it like this:
@Injectable({
providedIn: 'root',
useFactory() {
console.log('this useFactory should be called, but... nope :`(')
return {}
},
})
class TestService {
constructor() {
console.log('this constructor should not be called!!! but it's');
}
}
Using an arrow function, an anonymous function or an exported function work (see repro).
But using an arrow function angular language service
complainsβ¦ it works at runtime and building, but itβs annoying
π¬ Minimal Reproduction
this repro uses 9.1.1
, but it doesnβt work with latest (10.0.12
) either
https://ng-run.com/edit/zwh0lwIk3NYX9tKWSUMt
π₯ Exception or Error
π Your Environment
Angular Version:
Angular CLI: 10.0.7
Node: 14.6.0
OS: linux x64
Angular: 10.0.12
... animations, common, compiler, compiler-cli, core, forms
... localize, platform-browser, platform-browser-dynamic, router
Ivy Workspace: Yes
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.1000.4
@angular-devkit/build-angular 0.1000.7
@angular-devkit/build-optimizer 0.1000.7
@angular-devkit/build-webpack 0.1000.7
@angular-devkit/core 10.0.4
@angular-devkit/schematics 10.0.7
@angular/cdk 10.1.3
@angular/cli 10.0.7
@ngtools/webpack 10.0.7
@schematics/angular 10.0.7
@schematics/update 0.1000.7
rxjs 6.6.2
typescript 3.9.7
webpack 4.43.0
Anything else relevant?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Configuring dependency providers - Angular
The expanded provider configuration is an object literal with two properties: ... useFactory - allows you to define a function that constructs a...
Read more >Angular Providers: useClass, useValue, useFactory ...
It can create a dependency from the existing service class ( useClass ). It can inject a value, array, or object ( useValue...
Read more >Can't inject service into Angular Provider UseFactory
The answer, in my case, was very easy! The provider code needs a property called deps with the types of the dependencies I...
Read more >Dependency Injection in Angular - Parser
useFactory. The useFactory allows you to create a dependency object by calling a factory function. It is convenient if you don't know whatΒ ......
Read more >Dependency providers - Angular Hispano
Not all dependencies are classes. Sometimes you want to inject a string, function, or object. Apps often define configuration objects with lots of...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Useless functionality, can be removed
No problem, thanks for confirm! π