Reputation: 5522
I have a function in my service as follow:
export class DualLogonService {
url: string;
constructor(private router: Router) {}
saveURL(): void {
this.url = this.router.url;
}
}
App.module
import { DualLogonService } from './ dual-logon/dual-logon.service';
...
export function saveUrl(dualLogonService: DualLogonService, router: Router) {
console.log(router.url);TypeError: Cannot read property 'url' of undefined
return dualLogonService.saveURL();//TypeError: Cannot read property 'saveURL' of undefined
}
{
provide: APP_INITIALIZER,
deps: [DualLogonService],
useFactory: saveUrl,
multi: true
}
If I do the following int the service, the function is accessible in the app.module, but I can't use the this.router.url
:
export function saveURL() {
this.url = this.router.url;
}
Upvotes: 3
Views: 6331
Reputation: 737
//This function should be above the @ngModel decorator
export function callSaveUrl(dualLogonService: DualLogonService) {
//Do what you need here
return (): Promise<any> => {
return dualLogonService.saveUrl();
}
}
DualLogonService,
{ provide: APP_INITIALIZER,useFactory: callSaveUrl, deps: [DualLogonService], multi: true}
Try something like this, its untested but I think this is what you are looking for
Change your code from
{
provide: APP_INITIALIZER,
deps: [DualLogonService],
useFactory: saveUrl,
multi: true
}
To this
DualLogonService,
{ provide: APP_INITIALIZER,useFactory: callSaveUrl, deps: [DualLogonService], multi: true}
Upvotes: 5