Ngrx 在 http 请求后显示成功或错误消息或重定向的正确方法

Kno*_*ker 4 redux ngrx ngrx-effects angular ngrx-store

我有一个Ionic 3 应用程序,我使用ngrx/storengrx/effects来处理副作用或异步操作。一切正常,但有一个小问题。

我应该在调度操作后何时调用错误或成功消息。我知道效果工作是根据组件的分派动作分派另一个动作。但是我应该把它放在哪里?

这是我的reducer.ts下面的示例

mport { LOGIN_REQUEST, LOGIN_FAILED, LOGIN_SUCCESS } from './../actions/authenticate';
import { AuthState } from './../store';
import { Action } from '@ngrx/store';
import { PostState } from '../store';

const initialState: AuthState = {
    isAuthenticated: false,
    authenticating: false,
    token: null,
    error: null
}

export function authReducer(state = initialState, action: Action) {
    switch (action.type) {
        case LOGIN_REQUEST:
            return {
                ...state,
                authenticating: true,
                // USERS: (<any>action).data.USERS
            }
        case LOGIN_SUCCESS:
            return {
                ...state,
                authenticating: false,
                isAuthenticated: true,
                token: (<any>action).payload.data.token
            }
        case LOGIN_FAILED:
            return {
                ...state,
                authenticating: false,
                error: (<any>action).payload
            }
        default:
            return state;
    }
}
Run Code Online (Sandbox Code Playgroud)

并在我的effects.ts

@Effect()
    authenticate$ = this.actions$.ofType(authActions.LOGIN_REQUEST)
        .pipe(
            switchMap((data: any) => {
                return this.authApi.signIn(data.payload).pipe(
                    map((response: any) => ({ type: authActions.LOGIN_SUCCESS, payload: response })),
                    catchError(error => of({ type: authActions.LOGIN_FAILED, payload: error }))
                )
            })
        )
Run Code Online (Sandbox Code Playgroud)

在我的app.module

imports: [
    ...
    StoreModule.forRoot(rootReducer, { metaReducers }),
    EffectsModule.forRoot(effects),
    StoreDevtoolsModule.instrument({
      maxAge: 5
    })
  ],
Run Code Online (Sandbox Code Playgroud)

最后在我的component.ts

ionViewWillEnter() {
    this.store.select<any>('auth').subscribe(state => {
      this.auth = state
      if (state.error) this.displayErrorMessage()
    })
  }



    displayErrorMessage() {
    const toast = this.toastCtrl.create({
      message: 'Error occured',
      duration: 3000,
      position: 'top'
    });

    toast.onDidDismiss(() => {
      console.log('Dismissed toast');
    });

    toast.present();
  }
Run Code Online (Sandbox Code Playgroud)

If you are familiar with redux you knew that you would understand the code above in the reducers and effects. Everything is working fine but I think there is a little problem in my component on when to call a success or error message? Should I call it instead in the effect? But How?

Appreciate if someone could help with code sample. Thanks in advance.

tim*_*ver 5

您可以注入ActionsSubject组件并监听成功或失败的操作,但我不建议这样做。

也可以使用效果来显示通知,例如使用有角度的材料小吃店:

@Effect({ dispatch: false })
error = this.actions.pipe(
 ofType<ServerError>(ActionTypes.ServerError),
 map(({ payload }) => {
   this.snackBar.open(payload.message, 'Close');
 })
)
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅为此开始使用 ngrx/effects