2021年1月4日星期一

How to create inject existing created service created with useValue

I have a service that looks like this:

@Injectable()  export class EntitySearchService {    constructor(@Inject("resourse") private resourse: Resourse) {}      private currentService = ServiceLocator.get(EntityServiceFactory).getServiceInstance(this.resourse);    searchEntity(filter) {      this.currentService.setSearchFilter(filter).fetchWithFilter();    }      resetSearchPage(filtersToReset: Filters[]) {      const currentFilter = this.currentService.getFilter();      for (const filter of filtersToReset) {        delete currentFilter[filter];      }      this.currentService.setSearchFilter({ ...currentFilter, pageNumber: 1 }).fetchWithFilter();    }  }  

It is depending on resourse and has to get it as a parameter. I have exactly 2 resourses that use this service(potentially more). Each component that uses it inject it with useValue like this:

providers: [      EntitySearchService,      { provide: "resourse", useValue: Resourse.myResourse},    ],  constructor(      private entitySearchService: EntitySearchService,    ) {}  

This creates a new instance of Search Service, that fine but the problem is that when I also inject it in the child component of a component that registered the service as a provider with useValue, it creates another instance, and that is not what I want. I want to create this service only in parent and inject the same instance to the children.

How do I do that ?

https://stackoverflow.com/questions/65572282/how-to-create-inject-existing-created-service-created-with-usevalue January 05, 2021 at 10:06AM

没有评论:

发表评论