Я пытаюсь сделать это https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#bidirectional-serviceкомпонент взаимодействия через сервис Угловое 2
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>
И объявить "providers: [InteractionService]
" в app.module.ts.
Но в sidebar.component.ts У меня есть ошибка «Не удается прочитать свойство„режим“неопределенной».
Что делать неправильно? Как я могу это разрешить?
Что бы вы мне предложили? Сделайте тот же второй ajax-запрос в sidebar.component.ts, что и в app.component.ts? Я просто хочу уменьшить количество аякс-запросов в моем приложении. –
Что-то вроде http://stackoverflow.com/questions/36271899/what-is-the-correct-way-to-share-the-result-of-an-angular-2-http-network-call-in/36291681 # 36291681 –