0

I am trying to get the PLATFORM_ID on my super class but I'd rather not have to pass it from all classes extending it. I tried making it optional but it just came back as undefined.

This is in my base class

 constructor(
    public bucketType: 'quote' | 'list' | 'quote_cart' | 'cart',
    public http: HttpClient,
    public gaService: GaService,
    public gtmService: GtmDataLayerService,
    @Inject(PLATFORM_ID) private platformId?: string
) {
Tony Smith
  • 869
  • 1
  • 11
  • 25

1 Answers1

1

Your service could look like:

some.service.ts

let platform: string;

export function setPlatform(value) {
  platform = value;
}

@Injectable(...)
export class SomeService {

  constructor() {
    console.log(platform);
  }
}

Now all you need to do is to set platform variable only once:

app.module.ts

import { setPlatform } from './some.service';

@NgModule(...)
export class AppModule {
  constructor(@Inject(PLATFORM_ID) private platformId?: string) {
    setPlatform(platformId);
  }
}
yurzui
  • 205,937
  • 32
  • 433
  • 399