Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Rxjs链接,如何实现?_Angular_Typescript_Rxjs - Fatal编程技术网

Angular Rxjs链接,如何实现?

Angular Rxjs链接,如何实现?,angular,typescript,rxjs,Angular,Typescript,Rxjs,下面的代码就是我所拥有的,它可以工作 canActivate(route: ActivatedRouteSnapshot): Observable<UrlTree | boolean> { return new Observable<UrlTree | boolean>((observer) => { this._authorizedStore .select(selectProfile) .pipe(take(1))

下面的代码就是我所拥有的,它可以工作

canActivate(route: ActivatedRouteSnapshot): Observable<UrlTree | boolean> {
  return new Observable<UrlTree | boolean>((observer) => {
    this._authorizedStore
      .select(selectProfile)
      .pipe(take(1))
      .subscribe((profile) => {
        if (!profile) {
          this._authorizedStore.dispatch(
            AuthorizedActions.loadProfile({ userId: this._authenticationApi.getUser().id }),
          );
        }

        this._authorizedStore
          .select(selectAuthorizedState)
          .pipe(first((state) => !!state.profile))
          .subscribe((state) => {
            if (state.profile.profiles.length > 0) {
              observer.next(true);
            } else {
              observer.next(this.router.createUrlTree(['./settings']));
            }
            observer.complete();
          });
      });
  });
}
canActivate(路由:ActivatedRouteSnapshot):可观察{
返回新的可观察对象((观察者)=>{
这个
.选择(选择个人资料)
.管道(取(1))
.订阅((个人资料)=>{
如果(!配置文件){
这个(
Authorizations.loadProfile({userId:this.\u authenticationApi.getUser().id}),
);
}
这个
.选择(选择授权状态)
.pipe(第一个((状态)=>!!state.profile))
.订阅((状态)=>{
如果(state.profile.profiles.length>0){
观察者:下一个(正确);
}否则{
observer.next(this.router.createUrlTree(['./设置']);
}
observer.complete();
});
});
});
}
我想要的是找到一种更好、更漂亮的方法来做同样的事情。 基本上我想首先检查我是否有一个配置文件,如果我没有,我想触发请求,然后等待。 需要注意的一点是,正如您所看到的,我正在使用Ngrx,因此如果我在开始时不使用(1),我将得到一个无限循环(不是profile,makerequest,不是profile,makerequest…)

有什么想法吗?

你可以使用和。像这样:

this.\u authorizedStore
.选择(选择个人资料)
.烟斗(
以(1)为例,
轻触(配置文件=>{
如果(!配置文件){
这个(
Authorizations.loadProfile({userId:this.\u authenticationApi.getUser().id}),
);
}
}),
switchMap(profile=>this.\u authorizedStore.select(selectAuthorizedState.pipe)(第一个(state=>!!state.profile)))
)
.订阅(状态=>{
如果(state.profile.profiles.length>0){
观察者:下一个(正确);
}否则{
observer.next(this.router.createUrlTree(['./设置']);
}
observer.complete();
});
您还可以将整个代码缩短为:

canActivate(路由:ActivatedRouteSnapshot):可观察{
把这个还给我
.选择(选择个人资料)
.烟斗(
以(1)为例,
轻触(配置文件=>{
如果(!配置文件){
这个(
Authorizations.loadProfile({userId:this.\u authenticationApi.getUser().id}),
);
}
}),
switchMap(profile=>this._authorizedStore.select(selectAuthorizedState)),
首先(state=>!!state.profile),
映射(state=>state.profile.profiles.length>0?true:this.router.createUrlTree(['./设置']))
);
}

这是什么。_authorizedStore.dispatch(…loadProfile…)?承诺?它还回什么吗?看起来之后,您将等待状态首先有一个配置文件
(state=>!!state.profile)
。那两个人有联系吗?完美的迈克!谢谢!我试的时候错过了开关地图。