javascript - 操作必须具有类型属性

标签 javascript angular typescript ngrx

我的效果似乎抛出了一个操作必须具有类型属性,这似乎是我的RegisterSuccess操作导致了问题,但环顾四周,它可能正如我所说的那样,我尝试了他们的一些解决方案,但它们似乎不适用于我的情况。

效果:

 @Effect()
    register = this.actions$.pipe(
        ofType(AuthActionTypes.REGISTER),
        switchMap(action => this.auth.register(action.registration).pipe(
            map(result => ([
                { type: AuthActionTypes.REGISTER_SUCCESS, user: result }
            ])),
            catchError(result => ([
                { type: AuthActionTypes.REGISTER_FAIL }
            ])),
        ))
    );

行动:

export class Register implements Action {
    readonly type = AuthActionTypes.REGISTER;
    constructor(public registration: Registration) {}
}

export class RegisterSuccess implements Action {
    readonly type = AuthActionTypes.REGISTER_SUCCESS;
    constructor(public user: User) {}
}

export class RegisterFail implements Action {
    readonly type = AuthActionTypes.REGISTER_FAIL;
    constructor() {}
}

服务:

register(user: Registration): Observable<any> {
        return this.api.post('auth/register', user).pipe(map(res => res.data));
    }

最佳答案

您的效果是为 NgRx 版本 8.3.0 编写的 您的操作是为 NgRx 版本 7.4.0 编写的

“switchMap(action => this.auth.register(action.registration).pipe(” 你在这里不需要管道

对于 NgRx 版本 8.3.0 应该是这样的: 行动:

import { createAction, props } from '@ngrx/store';
export const register = createAction(AuthActionTypes.REGISTER());
export const registerSuccess = createAction(AuthActionTypes.REGISTER_SUCCESS, props<{user: User}>());
export const registerFail = createAction(AuthActionTypes.REGISTER_FAIL);

效果:

@Effect()
    register = this.actions$.pipe(
        ofType(register.type),
        mergeMap(action => this.auth.register(action.registration),
        map(result => ({ type: registerSuccess.type, user: result })),
        catchError(result => ({ type: registerFail.type })),
        ))
    );

关于javascript - 操作必须具有类型属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58125205/

相关文章:

javascript - 页面加载后由 ajax 或 javascript 检索的谷歌索引文本

javascript - 以 Angular 将文件转换为 uInt8Array

angular - 类型 'HTMLElement' 的参数不可分配给类型 'CanvasImageSource' 的参数

javascript - 直接向 mat-step 添加(点击)事件处理程序?

javascript - 为什么代码要这样转换?

javascript - 使用 JavaScript 编辑 AppData 文件夹中的文件

javascript - 如何定位div的子元素?

javascript - Jquery(输入/文本区域).val() : how is it adding content without changing the DOM?

angular - 如何显示所有其他用户的列表,其中包含来自 firebase 的详细信息(不包括当前登录的用户)?

javascript - 在 typescript 中使用传播语法和 new Set()