【发布时间】:2016-07-24 02:32:47
【问题描述】:
我正在尝试构建如下共享服务
import {Injectable,EventEmitter} from 'angular2/core';
import {Subject} from 'rxjs/Subject';
import {BehaviorSubject} from 'rxjs/subject/BehaviorSubject';
@Injectable()
export class SearchService {
public country = new Subject<SharedService>();
public space: Subject<SharedService> = new BehaviorSubject<SharedService>(null);
searchTextStream$ = this.country.asObservable();
broadcastTextChange(text: SharedService) {
this.space.next(text);
this.country.next(text);
}
}
export class SharedService {
country: string;
state: string;
city: string;
street: string;
}
我不知道如何实现 BehaviourSubject 基本上我在这里尝试的只是我猜的一团糟,我通过使用在子组件中调用这个值
console.log('behiob' + shared.space.single());
这会引发错误,因为 .single()/last() 等任何可用的都不是函数,所以有人可以向我展示它的实际工作原理以及在我搜索示例时如何实现它,但没有任何意义对我来说。
【问题讨论】:
标签: angular rxjs angular2-changedetection