我试图在状态更新之前在我的动作创建者中解决这个异步调用。我试图实现 redux thunk,但我对它和 angular4+ 整体来说还很陌生。
这是我的动作创建者的样子:
@Injectable()
export class IndexActions {
constructor(
private _cloudReadService: CloudReadService
) {}
static UPDATE_INDEX = 'UPDATE_INDEX';
updateIndex(): ActionWithPayload {
return {
type: IndexActions.UPDATE_INDEX,
payload: this._cloudReadService.getRecordsByOwner()
.then(response => {return response})
.catch(err => console.log)
}
}
}
thunk 的主要问题是我的服务方法没有在实际的 thunk 中定义。
以下是该服务的基本外观:
@Injectable()
export class CloudReadService {
constructor() {
}
getRecordsByOwner(): any {
return firebase.database()
.ref('lists/records/owners')
.orderByChild('ownerName')
.once('value')
.then(snapshot => {
/* process response */
return processedResponse;
}
})
}
}
我想我的问题真的是如何在 redux 中间件中使用服务方法,或者有其他方法吗?
非常感谢任何帮助。