'Actions'类型中不存在属性'payload'

时间:2016-11-24 05:21:02

标签: typescript visual-studio-code ngrx

我是TypeScript和Visual Studio Code的新手。 我收到以下错误:

*[ts] Property 'payload' does not exist on type 'Actions'.

我的代码:

action.ts文件:

  import { Action } from '@ngrx/store';
  import { Item } from '../models/list';

  export class AddBookAction implements Action {
      type = ActionTypes.ADD_BOOK;

      constructor(public payload: Item) { }
  }

  export type Actions = AddBookAction;

reducer.ts

import * as list from 'action';

export function reducer(state = initialState, action: list.Actions): State {

switch (action.type) {
    case list.ActionTypes.LOAD: {
      return Object.assign({}, state, {
        loading: true
      });
    }

    case list.ActionTypes.LOAD_SUCCESS: {
      const books = action.payload // Error here

      return {
        loaded: true,
        loading: false,
        ids: books.map(book => book.id)
      };
    }
}

任何建议都会非常有用。

1 个答案:

答案 0 :(得分:2)

您发布的代码看起来不完整,因为在 reducer.ts 中您使用的是list.ActionTypes.LOAD_SUCCESS,但这不是 action.ts 你发布了。

所以可能有一种猜测,那就是在LoadSuccessAction中你错过了payload - attirbute - 例如public payload: Item

另一个猜测是你忘了联合LoadSuccess - 输入:

export type Actions = AddBookAction | LoadSuccessAction;