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? 😀😀 - overthesanityComponentFactoryResolverfor services? - Stepan Kasyanenko 2:26 pmнекогда доки читать - надо работать- this is sacred, but you know, you won't get far without documentation, as my workshop says - RTFM 😁 there is anInjectorfor services, but it’s enough for a person to register a service in the constructor) - March