'What are the common use cases of angular's DI resolution modifiers?
I have studied resolution modifiers recently, but I do not understand where to use them in my project. It seems to me that I should only use them if, somehow, I have a service and I need different instances of it in different places. For instance:
@Component({
....
providers: [ServiceA]
})
class A {
constructor(@Self() private serviceA: ServiceA){}
}
What is the point of implementing self in this case, meaning that without it, this case would work just fine, right? :D
I would be glad to hear of your experience. <3
Solution 1:[1]
I'm trying to give the possible answer of your question. as below please check
Parameter decorators are to be used on constructor parameters, which tells DI to start dependency resolutions from local. like this..
class Dependency {}
@Injectable()
class NeedsDependency {
constructor(@Self() public dependency: Dependency) {}
}
let inj = Injector.create({
providers: [
{provide: Dependency, deps: []},
{provide: NeedsDependency, deps: [[new Self(), Dependency]]}
]
});
const nd = inj.get(NeedsDependency);
expect(nd.dependency instanceof Dependency).toBe(true);
const child = Injector.create({
providers: [{provide: NeedsDependency, deps: [[new Self(), Dependency]]}],
parent: inj
});
expect(() => child.get(NeedsDependency)).toThrowError();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Siddhi Patel |
