javascriptangularangular-materialmonoreponx-monorepo

How to know function call came from which application in angular monorepo nx?


In monorepo nx angular application I have 2 application A & B and a common shared component C. I've inserted C in both application using components app selector. I want to know apart from URL is there any way I can check if I clicked on that component from which application the call is being made and based upon application A or B want to return data or perform operations.


Solution

  • Create a custom injection token, which you can read from the component using DI, then you can know the origin.

    In the root of your application, either standalone or module. You need to add this to the providers array.

    export const ORIGIN_TOKEN = new InjectionToken<string>('originToken');
    
    @NgModule({
    ...
    providers: [{ provide: ORIGIN_TOKEN, useValue: 'A' /* or 'B' */ }],
    ...
    })
    

    Then in component C you can import it like below.

    ...
    public constructor(
       @Inject(ORIGIN_TOKEN) private fromWhere: string,
    ) {}
    ...
    

    Configure your Angular apps with an injection token