Iro*_*sun 13 rxjs angular2-changedetection angular
我正在尝试构建一个共享服务,如下所示
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;
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何实现BehaviourSubject基本上我在这里尝试的只是一团糟我猜我在使用子组件调用这个值
console.log('behiob' + shared.space.single());
Run Code Online (Sandbox Code Playgroud)
这是一个错误,因为.single()/ last()等等什么是可用的不是一个函数所以有人可以告诉我它是如何工作的以及如何实现它,因为我搜索的例子但没有一个对我有意义.
Gün*_*uer 20
减少到一个属性应该是这样的.我改为SharedService,string因为使用XxxService为事件值命名的类型对我没有意义:
import {Injectable} from 'angular2/core';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
@Injectable()
export class SearchService {
public space: Subject<string> = new BehaviorSubject<string>(null);
broadcastTextChange(text:string) {
this.space.next(text);
}
}
Run Code Online (Sandbox Code Playgroud)
@Component({
selector: 'some-component'
providers: [SearchService], // only add it to one common parent if you want a shared instance
template: `some-component`)}
export class SomeComponent {
constructor(searchService: SearchService) {
searchService.space.subscribe((val) => {
console.log(val);
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16943 次 |
| 最近记录: |