代码在这里:
@Injectable()
export class ProjectService {
create$: Rx.Subject<Response> = new Rx.Subject();
_create: Rx.Observable<Response> = this.create$.asObservable();
newProject: Rx.Subject<ProjectInfo> = new Rx.Subject();
get$: Rx.Subject<any> = new Rx.Subject();
_get: Rx.Observable<any> = this.get$.asObservable();
constructor(public _http: Http, public option: HeaderWithToken) {
this._create = this.newProject.flatMap(project => {
console.log("create",project);
return this._http.post(baseURL + "/project",
JSON.stringify(project), this.option.Option);
});
this._get = this._http
.get(baseURL + "/project", this.option.Option)
.do(x=>{
console.log("to get",x);
})
.map(res => res.json());
this._create
.map(x=>x.json())
.filter(res=>res.status==200)
.subscribe(this.get$);
this._get.subscribe(x => {
console.log("get:", x);
})
}
addProject(project: ProjectInfo) {
this.newProject.next(project);
}
getProject() {
return this._get;
}
}
我希望该流能够正常工作 1.当我调用addProject =&gt;发出值=&gt;触发帖子请求=&gt;当发布响应200继续获取请求时(_get stream)=&gt;我可以在其他地方订阅get $ stream来获取所有最新数据。
实际上是: 发布成功,但没有进入获取请求,似乎错误的代码
this._create
.map(x=>x.json())
.filter(res=>res.status==200)
.subscribe(this.get$);
请帮忙!
答案 0 :(得分:0)
我让它有效。
@Injectable()
export class ProjectService {
_create: Rx.Observable < Response > = new Rx.Observable();
newProject: Rx.Subject < ProjectInfo > = new Rx.Subject();
_get: Rx.Observable < any > = new Rx.Observable();
res$: Rx.Observable < any > = new Rx.Subject().asObservable();
constructor(public _http: Http, public option: HeaderWithToken) {
//any new value into the newProject will deliver to post and save as create stream
//to check the status code in the create stream to close dialog
this._create = this.newProject
.flatMap(
project => {
return this._http
.post(
baseURL + "/project",
JSON.stringify(project),
this.option.Option
);
});
this.res$ = this._create
.filter(res => res.status == 200)
.flatMap(x => {
DialogServices.getRef()
.then(x => {
x.dispose();
})
return this._get;
})
//For get all project from DB, will return an array of projectInfo
this._get = this._http
.get(baseURL + "/project", this.option.Option)
.map(res => res.json());
this.res$.subscribe(x => {
cacheProject = x;
console.log("cache", x, cacheProject);
getAllProjects.next(cacheProject);
})
}
addProject(project: ProjectInfo) {
this.newProject.next(project);
}
}