简体   繁体   English

如何从回调函数使用ngrx / angular5从效果返回?

[英]How can I return from an effect with ngrx / angular5 from a callback function?

My effect has: 我的效果是:

  @Effect()   /* sends request to httpService with params as login credentials on instance of loginAction. */
  login$: Observable<Action> = this.actions$
    .instanceOf(LoginActions.LoginAction)
    .switchMap(
      action => {
        return this.loginHttpService.login(action.payload)
          .map( (res: any) => {
                const firstName = res.firstName;
                const lastName = res.lastName;
                // const privileges = res.privileges
                const privileges = ['ViewDeliverableDefinitions', 'SubmitDeliverableInstances']
                this.tokenService.setPrivileges(privileges)
                console.log('observable')
                return Observable.create(observer => {
                  console.log('this is 1')
                  this.permissionsService.loadPermissions(privileges, () => {
                    console.log('this is 2')

                    // observer.next({
                    //   type: 'string'
                    // });
                    this.store.dispatch(new LoginActions.LoginSuccessAction({user: res}))
                    // return observer.complete();

                  })
                })
          })
          .catch( (e:any)  => {
            this.store.dispatch(new LoginActions.LoginFailureAction(true));

            return Observable.create(observer => {
              return observer.complete();
            }) 
        });
      });

However, nothing ever gets called from Observer.create . 但是, Observer.create不会调用任何东西。 What am I doing wrong? 我究竟做错了什么?

There are a couple of things that need to be updated. 有几件事需要更新。 First map needs to be a switchMap as previously suggested. switchMap ,第一个map必须是switchMap This is because an Effect must return an Observable that emits an Action . 这是因为Effect必须返回发出ActionObservable Doing so will cause the inner observable to be subscribed to. 这样做将导致内部观察对象被订阅。 Second, instead of dispatching the action manually, the Effect will do this for you and so you need to just emit a new action in each created observable. 其次,Effect将为您执行此操作,而不是手动分派操作,因此您只需在每个创建的可观察对象中发出一个新操作。

Updated code (minus comments): 更新的代码(减去注释):

@Effect()   /* sends request to httpService with params as login credentials on instance of loginAction. */
login$: Observable<Action> = this.actions$
  .instanceOf(LoginActions.LoginAction)
  .switchMap(
    action => {
      return this.loginHttpService.login(action.payload)
        .switchMap((res: any) => {
          const firstName = res.firstName;
          const lastName = res.lastName;
          const privileges = ['ViewDeliverableDefinitions', 'SubmitDeliverableInstances'];

          this.tokenService.setPrivileges(privileges);

          return new Observable<Action>(observer => {
            this.permissionsService.loadPermissions(privileges, () => {

              observer.next(new LoginActions.LoginSuccessAction({user: res}));
              observer.complete();
            });
          });
    })
    .catch( (e:any)  => {
      return Observable.of(new LoginActions.LoginFailureAction(true));
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Angular5效果中从回调返回? - How can I return from a callback in an Angular5 effect? 在Angular5中完成ngrx动作/效果后如何通知我? - How can I be notified when an ngrx action / effect is complete in Angular5? 如何使用 Angular 8 NgRx 函数方法发送另一个有效的动作? - How can I dispatch another action in effect with Angular 8 NgRx function method? 使用ngrx效果Angular5进行茉莉花登录测试 - jasmine login test using ngrx effect Angular5 如何将两个不同的状态从我的 ngrx 效果传递到我的服务功能? - How do I pass two different pieces of state from my ngrx effect to my service function? 如何从Angular 4中的回调绑定 - How can I bind from a callback in Angular 4 我如何将一个从http.post返回的简单字符串值设置为Angular5中的变量 - How can i set a simple string value returned from http.post to a variable in Angular5 如何使用angular5 material2从api填充数据? - How can i populate data from api using angular5 material2? 我如何编写NgRx 8效果来触发诺言中的动作 - How do i write an NgRx 8 Effect that triggers an action from a promise Angular NgRx - 当其他动作返回值时,从效果中分派一个动作 - Angular NgRx - dispatch an action from effect when other actions return a value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM