如何在ngrx / effects中创建局部效果

时间:2017-06-15 10:43:35

标签: angular typescript ngrx ngrx-effects ngrx-store

我怎样才能在ngrx / effects中做类似的事情:

// I want to handle a sign up effect
return this.actions$.ofType(user.SIGN_UP_SUCCESS)
   .map((action: user.SignUpSuccessAction) => action.payload)
   .mergeMap(() => {
      // then I have to call two other async actions to add new records
      return [
         new userOptions.Add(new UserOptionsModel());
         new userInfo.Add(new UserInfoModel());
      ];
   })

如何处理userOptions和userInfo操作的成功操作,然后重定向到Dashboard页面?如果我在user.SIGN_UP_SUCCESS序列之外调度userOptions.Add和userInfo.Add操作,例如从其他页面调度,我不想重定向到Dashboard页面。

ANSWER

维克多萨维金。 NgRx的状态管理模式和最佳实践 https://www.youtube.com/watch?v=vX2vG0o-rpM

2 个答案:

答案 0 :(得分:2)

您可以使用@ngrx/router-store

return this.actions$.ofType(user.SIGN_UP_SUCCESS)
   .mergeMap(() => {
      // then I have to call two other async actions to add new records
      return [
         new userOptions.Add(new UserOptionsModel()),
         new userInfo.Add(new UserInfoModel()),
         go(['/path', { routeParam: 1 }], { query: 'string' });
      ];
   })

答案 1 :(得分:0)

2个选项:

首先:

return this.actions$.ofType(user.SIGN_UP_SUCCESS)
   .mergeMap(() => {
      // then I have to call two other async actions to add new records
      return [
         new userOptions.Add(new UserOptionsModel());
         new userInfo.Add(new UserInfoModel());
      ];
   })
   .map((action) => {
      if(action instanceof UserInfoModel){
        /// latest
      }
   })

第二

您可以使用map两次而不是mergeMap

return this.actions$.ofType(user.SIGN_UP_SUCCESS)
       .map(toPayload)
       .map(() => {
          return new userOptions.Add(new UserOptionsModel());                 
       })
       .map(() => {
             return new userInfo.Add(new UserInfoModel());
       })
       .map(() => {
             //the end
       })

**** map(toPayload)执行此操作:map((action: user.SignUpSuccessAction) => action.payload)

import { toPayload } from '@ngrx/effects'

度过美好的一天: - )