I want to create a service in which an object of angular components are stored. That service basically would have a an property components which would look like {key: componentClass}.
It seems pretty simple to implement and it actually is, but I have faced one problem and I don't know how to solve it.
Lets call service ComponentStoreService. As an example it could look like this:
@Injectable()
export class ComponentStoreService {
 components: { [key: string]: any; } = {};
   register(key: string, components: any): void {
     this.components[key] = components;
   }
   getByKey(key: string): any {
     return this.components[key];
   }
   getAll(): any {
     return this.components;
   }
}
and now lets create 2 demo components and store them.:
const providers = ReflectiveInjector.resolve([ComponentStoreService]);
const injector = ReflectiveInjector.fromResolvedProviders(providers);
const componentStore= injector.get(ComponentStoreService );
@Component({
  selector: 'component-1',
  template: `<h2>component-1</h2>`
})
export class D1Component {constructor() {}}
componentStore.register('component-1', D1Component );
console.log('component-1 called', componentStore.getAll())
2nd one:
    const providers = ReflectiveInjector.resolve([ComponentStoreService]);
    const injector = ReflectiveInjector.fromResolvedProviders(providers);
    const componentStore= injector.get(ComponentStoreService );
    @Component({
      selector: 'component-2',
      template: `<h2>component-2</h2>`
    })
    export class D2Component {constructor() {}}
    componentStore.register('component-2', D2Component );
    console.log('component-2 called', componentStore.getAll())
And as a result first console.log prints object in which is first component added. Which is ok. And second console.log prints only second component. Which is incorrect.
As far as I understood in each component ComponentStoreService is separate instance. How I could make it singleton in whole app? I'm registering ComponentStoreService only in the app level. It means that the problem is elsewhere. Maybe it's because I'm calling service outside component class.
 
    