K
K
Konstantin2020-10-09 16:10:33
Angular
Konstantin, 2020-10-09 16:10:33

How to use an alias on an Angular class?

I have a class that receives a different value of the type parameter (1,2,3) as input to the constructor, inside the class a request is made to the server with this type type. The data is returned.

I need to have 3 instances of this class in the component, for this I requested them:

providers: [
   { provide: Search, deps: [CLOUD_SERVICE, SEARCH_CONFIG_ONE] },
    { provide: Search, deps: [CLOUD_SERVICE, SEARCH_CONFIG_TWO] },
     { provide: Search, deps: [CLOUD_SERVICE, SEARCH_CONFIG_THIRD] }
]


How to inject each provider into the component's constructor with its own alias, like so:

constructor(public search: Search as SearchOne,  public searchtwo: Search as SearchTwo, public searchThird: Search as SearchThird) {
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
LastDragon, 2020-10-24
@Junart1

I need to have 3 instances of this class in the component, for this I requested them:

You can add `multi: true` to all and then:
constructor(@Inject(Search) public search: Search[]){}

https://angular.io/guide/dependency-injection-prov...

A
Anton Shvets, 2020-10-09
@Xuxicheta

It looks like you need an abstract class and three of its implementations, and expose them under their names.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question