带有Redux-Thunk的Typescript给出错误

时间:2017-05-01 21:33:17

标签: angular typescript redux-thunk

我刚刚接手了Angular 2 / Redux项目。不幸的是,我不太了解Angular 2。我正在尝试使用npm start引导项目,但收到此错误:

app/_dashboard.store.ts(54,37): error TS2345: Argument of type '(dispatch: any) => void' is not assignable to parameter of type 'Action'.
  Property 'type' is missing in type '(dispatch: any) => void'.

这是导致错误的代码:

export const getNotificationAlerts: Function = () => {

let url = '/myaccount/GetUserProfileAlertSettings';

post(url, {}, true)
    .then(function (response) {
        DashboardStore.dispatch((dispatch) => {
            if (response.data) {
                let notifications = response.data;

                delete notifications.ResultCode;

                dispatch({
                    type: 'STORE_NOTIFICATIONS',
                    notifications: notifications
                });
            }
        });
    })
    .catch(function (error) {
        DashboardStore.dispatch((dispatch) => {
            dispatch({
                type: 'ERROR_STORE_NOTIFICATIONS',
                error: error
            });
        });
    });
}

组件:

@Component({
moduleId: module.id,
selector: 'dashboard-notifications',
templateUrl: '../Templates/dashboard/notifications.html',
animations: [
    trigger('notificationVisibility', [
        state('true', style({ opacity: 1, display: 'block' })),
        state('false', style({ opacity: 0, display: 'none', height: '0px' })),
        transition('*=>*', animate('0.15s'))
    ])
]
})
export class DashboardNotificationsComponent {

// Initialize variables
@Input() data: any;
//showNotifications = 'true';
notificationActionSelected = false;

constructor() { }

processDetailsApproval: Function = (val, sender) => {

    let thisComponet = this;

    // Set disappear flag for class
    this.notificationActionSelected = true;

    if (typeof val !== 'undefined') {
        processSenderApproval({
            'Status': val,
            'RecipientId': sender.RecipientId,
            'BusinessId': sender.BusinessId
        });

    }

    setTimeout(function () {
        thisComponet.notificationActionSelected = false;
    }, 500);

}
}

发布功能

export const post = (url: string, data: Object, includeToken) => {

    let params: Object = {};

    if (includeToken) {
        params = addToken({});
    }

    if (data) {
        params = Object.assign({}, params, data);
    }

    params = qs.stringify(params);

    return axios.post(url, params);

}

我花了一整天的时间来寻找解决方案,但是由于我没有经过多少培训就接管了这个项目,我很不确定我需要搜索什么,甚至,答案对我来说没有意义。任何线索都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

getNotificationAlerts函数应该为redux-thunk返回另一个函数。我没有使用ng2-redux但只使用angular1 redux。 Mabe这会起作用吗?

export const getNotificationAlerts: Function = () => {

   let url = '/myaccount/GetUserProfileAlertSettings';

   return post(url, {}, true)
   ...
 }