Search code examples
angularangular-cliangular-cli-v9

Pass data to Service from main.ts file in Angular application


I have a main.ts file in my Angular CLI application which contains the following export.. By the way, it's a microservice which gets CONTEXT from another microservice

export default {
    async mount({ rootElement, context }: Extension) {
        // Here I need to pass data to MyService

        platformBrowserDynamic()
            .bootstrapModule(AppModule)
            .catch((err) => console.error(err));
    },
};

I also have service

MyService {
  context: any;
}

I need to initialize it with the Context from main.ts file. I can't do it in any other place, only here because I get data from another extension only in main.ts file.

Do you have any idea?


Solution

  • you could try to pass it in providers

    platformBrowserDynamic()
                .bootstrapModule(AppModule, {providers: [{provide: CONTEXT, useValue: context}]})
                .catch((err) => console.error(err));
    

    and then just inject it wia the same token

    class MyAnything {
      constructor(@Inject(CONTEXT) private context: any)
    }