行为主体RxJs angular2

行为主体RxJs angular2,angular,rxjs,Angular,Rxjs,我没有在带有BehaviorSubject的组件中获得响应的值。我拿到了,并通过了服务 我正在从route guard上的服务触发HTTP 服务: @Injectable() export class ProjectsService { projects: Subject<Project[]> = new BehaviorSubject<Project[]>([]); load(clientId: string, active: boolean): Observable

我没有在带有BehaviorSubject的组件中获得响应的值。我拿到了,并通过了服务

我正在从route guard上的服务触发HTTP

服务:

@Injectable()
export class ProjectsService {
projects: Subject<Project[]> = new BehaviorSubject<Project[]>([]);

load(clientId: string, active: boolean): Observable<boolean> {
    return new Observable<boolean>((obs) => {
        this._api.send(active ? 'projects.getActive' : 'projects.getAll',clientId).subscribe(
            res => {
                let temp = [];
                res.forEach(a => temp.push(new Project(a)));

                if (active) this.projects.next(temp);
                else this.externalProjects.next(temp);
                obs.next(true);
                obs.complete();
            },

            err => {
                obs.next(false);
                obs.complete();
            }
        );
    });
}
HTML:

我也尝试了一种
NgZone
,但我不确定这是不是一种正确的方法:

load(clientId: string, active: boolean): Observable<boolean> {
    return this._api.send(active ? 'projects.getActive' : 'projects.getAll',clientId)
        .map(res => {
            let temp = res.map(a => new Project(a));

            this._ngZone.run(() => {
                console.log('temp: ', temp);
                if (active) this.projects.next(temp);
                else this.externalProjects.next(temp);
            });

            return true;
        })
        .catch(() => Observable.of(false));
}
load(clientId:string,active:boolean):可观察{
返回此。\u api.send(活动?'projects.getActive':'projects.getAll',clientId)
.map(res=>{
让temp=res.map(a=>newproject(a));
这个。_ngZone.run(()=>{
console.log('temp:',temp);
如果(活动)此项目下一个(临时);
否则,这个.externalProjects.next(temp);
});
返回true;
})
捕获(()=>可观察到的(假));
}

首先:不要订阅observable中的另一个observable。而是将其作为流写入:

@Injectable()
export class ProjectsService {
projects: Subject<Project[]> = new BehaviorSubject<Project[]>([]);

load(clientId: string, active: boolean): Observable<boolean> {
    return this._api.send(active ? 'projects.getActive' : 'projects.getAll',clientId)
        .map(res => {
                let temp = res.map(a => new Project(a));

                if (active) {
                    this.projects.next(temp);
                } else {
                    this.externalProjects.next(temp);
                }
                return true;
            })
       .catch(() => Observable.of(false));
}
模板:

<div class="project" [hidden]="!showIntro" *ngFor="let project of (projects$ | async)>
  {{project.title}}
</div>
....somewhere else:
<div *ngIf="showIntro$ | async">INTRO!</div>

您遇到了什么问题?您可以提到,您得到了什么错误。在哪里调用了
load()
?为什么要使用这么多嵌套的观测值?我没有得到一个错误。。。只是不渲染任何数据。。。无法在组件中获取HTTP响应。load()在route guard(canActivate)上正确调用。首先,我感谢您的努力,谢谢。我尝试了你写的所有东西。你说的“在可观察的内部可观察”是对的。我没有提供我在Ngondestory退订的组件的所有代码。API是干净的,当我
console.log(temp)
在服务中时,我得到了我想要的数据,但不是在我得到空数组的组件中。我在控制台中没有错误,所以我不知道该做什么,任何建议都会帮助我。我不知道这是否无关紧要,但我雄心勃勃地将这个应用程序从rc6升级到4.0.0-beta.0,除了这个行为主题外,一切都很好。我也尝试了
NgZone
,但不确定这是一个正确的方法。。。检查主post编辑…您只需要在ngZone.run中包装.next调用-这应该已经完成了,不需要很多层回调等。)但是,它仍然不起作用。我
console.log(projects$)
在一些随机点击按钮上,所以我提供了一个屏幕截图。我成功地让它工作,但我需要将包括angular在内的许多包降级到2.1。。。仍然没有得到它哪一个是打破它没有错误。。。。
load(clientId: string, active: boolean): Observable<boolean> {
    return this._api.send(active ? 'projects.getActive' : 'projects.getAll',clientId)
        .map(res => {
            let temp = res.map(a => new Project(a));

            this._ngZone.run(() => {
                console.log('temp: ', temp);
                if (active) this.projects.next(temp);
                else this.externalProjects.next(temp);
            });

            return true;
        })
        .catch(() => Observable.of(false));
}
@Injectable()
export class ProjectsService {
projects: Subject<Project[]> = new BehaviorSubject<Project[]>([]);

load(clientId: string, active: boolean): Observable<boolean> {
    return this._api.send(active ? 'projects.getActive' : 'projects.getAll',clientId)
        .map(res => {
                let temp = res.map(a => new Project(a));

                if (active) {
                    this.projects.next(temp);
                } else {
                    this.externalProjects.next(temp);
                }
                return true;
            })
       .catch(() => Observable.of(false));
}
public projects$: Observable<Project[]> = this._projectsService.projects;
public showIntro$: Observable<boolean> = this.projects$
    .map(projects => projects.length > 0);

ngOnInit(): void {
    // ...nothing to do here
}
<div class="project" [hidden]="!showIntro" *ngFor="let project of (projects$ | async)>
  {{project.title}}
</div>
....somewhere else:
<div *ngIf="showIntro$ | async">INTRO!</div>