In this component, I create a modal window using DialogService. After confirmation, the confirm method is called, where the setName method of the SocketService service is called. When starting the application, an error appears

ERROR TypeError: Cannot read property 'setName' of undefined


How to use two services, if only one can be registered in the constructor?
Component Code:

export interface ConfirmModel{ title: string; content: string; } @Component({ selector: 'app-confirm', templateUrl: './confirm.component.html', styleUrls: ['./confirm.component.css'] }) export class ConfirmComponent extends DialogComponent<ConfirmModel, boolean> implements ConfirmModel{ title: string; content: string; edited: boolean = true; name: string = ''; private socketService: SocketService; constructor(dialogService: DialogService){ super(dialogService); } confirm(){ this.result = true; this.socketService.setName(this.name); this.close(); } close(){ this.edited = false; } } 

Code SocketService:

 @Injectable({ providedIn: 'root' }) export class SocketService { private host: string="http://192.168.0.106:8080"; private socket: any = io(this.host); sendMessage(message){ this.socket.emit('callDriver', message); } setName(name){ this.socket.emit('setName', name); } getName(){ let observable = new Observable<string>(observer => { this.socket.on('getName', (name) => { observer.next(name); }); return () => { this.socket.disconnect(); }; }) return observable; } getMessages() { let observable = new Observable(observer => { this.socket.on('sendToDriver', (message) => { observer.next(message); }); return () => { this.socket.disconnect(); }; }) return observable; } constructor() { } } 
  • Как воспользоваться двумя сервисами, если в конструкторе можно прописать только один? - why do you think so? 😀😀 - overthesanity
  • You can inject several services. Who does not allow you to use a few? - Stepan Kasyanenko
  • @StepanKasyanenko, no one likes to read docks for the basics, but we always undertake to study all sorts of things, etc. over - overthesanity 2:23 pm
  • @overthesanity there is no time to read docks - you have to work! Can you write the answer, which will show how to inject the service not through the component constructor? Is there something like a ComponentFactoryResolver for services? - Stepan Kasyanenko 2:26 pm
  • @StepanKasyanenko there is no некогда доки читать - надо работать - this is sacred, but you know, you won't get far without documentation, as my workshop says - RTFM 😁 there is an Injector for services, but it’s enough for a person to register a service in the constructor) - March

1 answer 1

If you cannot inject more than one service in the constructor, then you can create an instance of this service yourself!

 export interface ConfirmModel{ title: string; content: string; } @Component({ selector: 'app-confirm', templateUrl: './confirm.component.html', styleUrls: ['./confirm.component.css'] }) export class ConfirmComponent extends DialogComponent<ConfirmModel, boolean> implements ConfirmModel{ title: string; content: string; edited: boolean = true; name: string = ''; private socketService: SocketService; constructor(injector: Injector, dialogService: DialogService){ super(dialogService); this.socketService = injector.get(SocketService); } confirm(){ this.result = true; this.socketService.setName(this.name); this.close(); } close(){ this.edited = false; } }