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

时间:2018-07-23 16:31:21

标签: angular ngrx ngrx-effects

我的效果是:

  @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();
            }) 
        });
      });

但是,Observer.create从未调用任何内容。我在做什么错了?

1 个答案:

答案 0 :(得分:1)

有几件事需要更新。如先前建议,第一个● rc-local.service - /etc/rc.local Compatibility Loaded: loaded (/lib/systemd/system/rc-local.service; static; vendor preset: enabled) Drop-In: /lib/systemd/system/rc-local.service.d └─debian.conf /etc/systemd/system/rc-local.service.d └─ttyoutput.conf Active: failed (Result: exit-code) since Tue 2018-07-31 01:56:25 CST; 4min 19s ago Process: 543 ExecStart=/etc/rc.local start (code=exited, status=2) Jul 31 01:56:24 raspberrypi systemd[1]: Starting /etc/rc.local Compatibility... Jul 31 01:56:25 raspberrypi rc.local[543]: connect: Network is unreachable Jul 31 01:56:25 raspberrypi systemd[1]: rc-local.service: Control process exited, code=exited status=2 Jul 31 01:56:25 raspberrypi systemd[1]: Failed to start /etc/rc.local Compatibility. Jul 31 01:56:25 raspberrypi systemd[1]: rc-local.service: Unit entered failed state. Jul 31 01:56:25 raspberrypi systemd[1]: rc-local.service: Failed with result 'exit-code'. Warning: rc-local.service changed on disk. Run 'systemctl daemon-reload' to reload units. 必须是map。这是因为switchMap必须返回发出Effect的{​​{1}}。这样做将导致内部观察对象被订阅。其次,Effect会为您执行此操作,而不是手动分派动作,因此您只需在每个创建的可观察对象中发出一个新动作即可。

更新的代码(减去注释):

Observable