简体   繁体   English

如何使用 Angular 8 NgRx 函数方法发送另一个有效的动作?

[英]How can I dispatch another action in effect with Angular 8 NgRx function method?

My develop environment does not indicate a problem (full green), but an error occurs when I run it.我的开发环境没有提示问题(全绿),但是运行的时候出现错误。 (I will get http404 and cacheError will run. It is fine now like "error test".) (我会得到 http404 并且 cacheError 会运行。现在就像“错误测试”一样很好。)

Error message: ERROR TypeError: "You provided '() => ({ type })' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. I see but I would like to dispatch loginFailed a action without props.错误消息: ERROR TypeError: "You provided '() => ({ type })' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.我明白了,但我想调度loginFailed操作没有道具。

HttpIntercept is exist! HttpIntercept 存在! So url will be http://localhost:PORT/user/login .所以 url 将是http://localhost:PORT/user/login Just it is not available.只是它不可用。

actions.ts动作.ts

// imports

export const types = {
    loginStart: '[User] Login start',
    loginSuccess: '[User] Login success',
    loginFailed: '[User] Login fail',
};

export const loginStart = createAction(types.loginStart, props<ILogin>());
export const loginSuccess = createAction(types.loginSuccess, props<IUser>());
export const loginFailed = createAction(types.loginFailed);

effect.ts效果.ts

import { types } from './actions';
import * as fromActions from './actions';
// and more imports

@Injectable()
export class UserEffects {
    constructor(
        private actions$: Actions,
        private http: HttpClient,
        private router: Router,
    ) {
    }

    loginProcess = createEffect(() => this.actions$.pipe(
        ofType(types.loginStart),
        exhaustMap((action) => {
            return this.http.post<IUser>('/user/login', {
                username: action.username,
                password: action.password,
            }).pipe(
                map(data => {
                    console.log(data);
                    return fromActions.loginSuccess(data);
                }),
                catchError((err) => { // this will run
                    console.log(err);
                    return fromActions.loginFailed;
                }),
            );
        })
    ));
}

catchError expects you to return an observable so perhaps try something like this using of : catchError期望您返回一个可观察的对象,因此可以尝试使用of

catchError((err) => { // this will run
    console.log(err);
    return of(fromActions.loginFailed());
}),

It's odd that you're not getting a error at compile time, but I think it might be because the RxJS catchError operator expects an observable to be returned.奇怪的是你在编译时没有收到错误,但我认为这可能是因为 RxJS catchError操作符期望返回一个可观察的。

Something like =>类似的东西 =>

catchError(err =>
  of(featureActions.LoadFailureAction({ err }))
),

Ran into this issue a while back and found this article from Wes Grimes (Google Developer Expert in Angular and NgRx core team member) to be really helpful.不久前遇到了这个问题,发现 Wes Grimes(Google Angular 开发专家和 NgRx 核心团队成员)的这篇文章非常有帮助。 https://itnext.io/ngrx-best-practices-for-enterprise-angular-applications-6f00bcdf36d7 https://itnext.io/ngrx-best-practices-for-enterprise-angular-applications-6f00bcdf36d7

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM