[英]Injecting services in Redux Epic Angular JS
我在AngularJS v1.6中为Redux功能使用“ redux-observable”。 以下代码用于史诗
export const getAllBidsAPI : any = (action$: ActionsObservable<any>, store: any, { customerService = CustomerService }) =>
action$.ofType(GET_ALL_BID)
.mergeMap((action : any) =>
Observable.fromPromise(customerService.getCustomers())
.map((res: any) => ({type : GET_ALL_BID_SUCCESS , payload : res.data.bid}))
);
这里不识别CustomerService。 我猜,它没有注入。
const rootEpic = combineEpics(
getAllBidsAPI
);
const rootReducer = combineReducers({
bidReducer
});
const epicMiddleware = createEpicMiddleware(rootEpic, {
dependencies : {CustomerService}
});
export const store = createStore(
rootReducer,
applyMiddleware(epicMiddleware)
);
这是我们在Epic中将CustomerService添加为依赖项的地方。 但是,我得到了错误。
无法读取未定义的属性“ getCustomers”
在redux-observable中undefined
依赖项的最常见原因是它实际上是作为undefined
提供的; 通常是因为导入/要求不正确。 仔细检查您是否正确导入了CustomerService
,并在将调试器传递到createEpicMiddleware
的文件中暂停了调试器,以确认它是(或不是) undefined
。
我看到的另一件事是他们使用的旧版本的redux-observable还不支持依赖项注入。 但是我非常有信心这是上面的第一个建议,因为redux-observable在默认情况下不提供任何东西作为您的史诗的第三个参数。 因此,在您的情况下,如果应归咎于redux-observable,您实际上会得到Cannot read property 'customerService' of undefined
因为第三个参数将为空,而是您提供的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.