I have the following exported function (out of class) that is defined in my AppComponent:
export function multiTranslateHttpLoaderFactory(http: HttpClient) {
return new MultiTranslateHttpLoader(http, [
{prefix: './assets/i18n/default/', suffix: '.json'},
{prefix: './assets/i18n/bc/', suffix: '.json'}
]);
}
This is then used within the import arrays in the AppModule in this way:
TranslateModule.forRoot ({
loader: {
provide: TranslateLoader,
useFactory: multiTranslateHttpLoaderFactory,
deps: [HttpClient]
}
}),
I would need a way to use my AuthService within the exported function, since I need certain properties to implement logic.
Is there a possibility for that?
For example, I would like to use my authService in this way:
export function multiTranslateHttpLoaderFactory(http: HttpClient) {
let bc = this.authService.activeBusinessCase$.getValue();
if(bc){
...
}else{
return new MultiTranslateHttpLoader(http, [
{prefix: './assets/i18n/default/', suffix: '.json'},
{prefix: './assets/i18n/bc/', suffix: '.json'}
]);
}
}
@Inject() is a manual mechanism for letting Angular know that a parameter must be injected. Injecting ChatWidget component to make the component behave like a singleton service so that the component state remain same across the app.
There are three types of Dependency Injections in Angular, they are as follows: Constructor injection: Here, it provides the dependencies through a class constructor. Setter injection: The client uses a setter method into which the injector injects the dependency.
Nothing easier.
export function multiTranslateHttpLoaderFactory(http: HttpClient, auth: AuthService) {
// add AuthService logic
return new MultiTranslateHttpLoader(http, [
{prefix: './assets/i18n/default/', suffix: '.json'},
{prefix: './assets/i18n/bc/', suffix: '.json'}
]);
}
and pass it as dependency
TranslateModule.forRoot ({
loader: {
provide: TranslateLoader,
useFactory: multiTranslateHttpLoaderFactory,
deps: [HttpClient, AuthService]
}
}),
Additional to this solution, you can also make use of the Angular's Injector API.
Follow the docs here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With