Is there a different way to invoke a function from a service class without doing it in the constructor? I attempted to call the function outside of the constructor, but I wasn't able to get the result of ShowDanger from Toast.service.ts
For example:
toast.service.ts
export class ToastService {
toasts: any[];
show(textOrTpl: string | TemplateRef<any>, options: any = {}) {
if (!this.toasts) {
this.toasts = [];
}
this.toasts.push({ textOrTpl, ...options });
}
showDanger(dangerTpl: string | TemplateRef<any>) {
this.show(dangerTpl, { classname: 'bg-danger text-light', delay: 5000 });
}
Parent.service.ts
constructor(
private toastService = ToastService
)
get<T>(queryParams?, customEndpoint?: string): Observable<T> {
let httpUrl = this.httpUrl;
this.http.get<T>(
`${httpUrl}`,
{ observe: 'response' }
)
.subscribe(resp => {
},
err => {
if (err.status === 400) {
this.toastsService.showDanger('Input Error');
} else if (err.status === 500) {
this.toastsService.showDanger('Communication Error');
} else {
this.toastsService.showDanger('Network Issues');
}
}
);
}
Child.service.ts
export class ChildService extends ParentService {
constructor(toastsService: ToastService) {
super(toastsService);
}
}