Angular ngrx /存储单向数据流

时间:2018-04-18 12:38:40

标签: angular redux ngrx ngrx-store

关于我尚未理解的管道问题。

以下管道是正确的吗?

app.component --> store.select(user).subscribe(loggedUser => { got to home })
app.component --> store.select(loginErrorMessage).subscribe(errorMessage => { show error message })
app.component.login() 
     --> user.service.login(user, password) 
           --> if (login OK) new ActionLogin(loggedUser)
                --> authReducer(state, action ) return {...state, user:action.payload.loggedUser}
           --> else new ActionWrongLogin(errorMessage)
                --> authReducer(state, action ) return {...state, loginErrorMessage:action.payload.errorMessage}

或者这个更好:

app.component --> store.select(user).subscribe(loggedUser => { got to home })
app.component --> store.select(loginErrorMessage).subscribe(errorMessage => { show error message })
app.component.login() 
     --> new ActionDoLogin(user, password)
           --> user.service.login(user, password) 
                  --> if (login OK) new ActionLogin(loggedUser)
                        --> authReducer(state, action ) return {...state, user:action.payload.loggedUser}
                  --> else new ActionWrongLogin(errorMessage)
                        --> authReducer(state, action ) return {...state, loginErrorMessage:action.payload.errorMessage}

1 个答案:

答案 0 :(得分:2)

根据您的评论,您可以致电service directly in component。没有必要为此付出额外的努力。

在第二个选项中,结构看起来像

onClickLogin -> 
  DoActionLogin ->
    Service.login()

DoActionLogin的唯一目的是调用另一种方法,因此应该将其删除。第一种选择更好。