【问题标题】:Angular ngrx Property 'payload' does not exist on type 'Action'“动作”类型上不存在 Angular ngrx 属性“有效负载”
【发布时间】:2020-06-24 14:04:18
【问题描述】:

我刚刚从 Angular 8 更新到 Angular 9,我遇到了一些我不太了解的问题。我看到了一些看起来与我的问题相似但没有帮助我解决任何问题的帖子(可能是因为我而不是其他人给出的解决方案)

所以,问题是当我启动 ng test 我有这个错误:

错误 TS2339:“操作”类型上不存在属性“有效负载”。

问题出在spy.calls.allArgs()[1][0].payload.user 上。就像setUserspy.calls.allArgs 之间的链接不存在了一样。

login.service.spec.ts

it('should get token', () => {
    const store = TestBed.inject(Store);
    const spy = spyOn(store, 'dispatch').and.callThrough();

    service.getToken('root', '', 'MY_DATABASE').subscribe(() => {
      expect(spy).toHaveBeenCalled();
      spy.calls.allArgs()[1][0]);
      const user: User = spy.calls.allArgs()[1][0].payload.user;
      expect(user.database).toBe('MY_DATABASE');
    });
  });

login.actions.ts

export class SetUser implements Action {
  readonly type = LOGIN_ACTION_TYPES.SET_USER;

  constructor(public payload: SetUserActionInterface) {}
}

interface SetUserActionInterface {
  user: User;
}

我的代码有什么问题?

【问题讨论】:

    标签: javascript angular ngrx ngrx-store


    【解决方案1】:

    我不知道这是否是一个好方法,但请尝试:

    // we are casting it to be of type SetUser where payload should be there.
    const user: User = (spy.calls.allArgs()[1][0] as SetUser).payload.user;
    

    【讨论】:

      猜你喜欢
      • 2018-01-06
      • 2017-04-08
      • 1970-01-01
      • 2018-08-06
      • 2021-06-28
      • 2018-04-25
      • 2018-07-12
      • 1970-01-01
      • 2018-05-01
      相关资源
      最近更新 更多