I am attempting to implement bidirectional service communication using Angular. I have followed the instructions provided in the documentation here: https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-service
interaction.service.ts:
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { Options } from './options';
@Injectable()
export class InteractionService {
private optionsSource = new Subject<Options>();
options$ = this.optionsSource.asObservable();
setOptions(options: Options) {
this.optionsSource.next(options);
}
}
app.component.ts:
import { Component, OnInit } from '@angular/core';
import { CoreService } from './core/core.service';
import { InteractionService } from './providers/interaction.service';
import { Options } from './options';
@Component({
selector: 'app',
templateUrl: './app/app.component.html'
})
export class AppComponent implements OnInit {
constructor(private coreService: CoreService,
private interactionService: InteractionService) { }
ngOnInit() {
this.coreService.getOptions().subscribe((options: Options) => {
this.interactionService.setOptions(options);
});
}
}
sidebar.component.ts:
import { Component, OnInit} from '@angular/core';
import { InteractionService } from './../../providers/interaction.service';
import { Options } from './options';
@Component({
selector: 'app-sidebar',
templateUrl: './app/core/sidebar/sidebar.component.html'
})
export class SidebarComponent implements OnInit {
options: Options;
constructor(private interactionService: InteractionService) {
this.interactionService.options$.subscribe(
(options: options) => {
this.options = options;
});
}
ngOnInit() {
if (this.options.mode === "test") {
//...
} else {
//...
}
}
}
app.component.html:
<app-sidebar></app-sidebar>
<router-outlet></router-outlet>
I have added "providers: [InteractionService]
" to app.module.ts.
However, when running the code, I encountered an error in sidebar.component.ts stating "Cannot read property 'mode' of undefined". What could be causing this issue and how can it be resolved?