0

我在AngularJS v1.6中對Redux功能使用「redux-observable」。 下面的代碼是一個史詩在Redux中注入服務Epic Angular JS

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作爲依賴關係的地方。但是,我得到錯誤。

無法讀取的不確定

+0

你如何解決這種依賴關係? – Hitmands

+0

@Hitmands解決依賴關係的確切意思是什麼? –

+1

AngularJS(1. *)具有'$ injector'對象,這意味着您只需註冊依賴關係並僅在框架允許的情況下(組件,服務,控制器等)使用它們。那麼,你如何解決'customerService'小寫?你做了:'dependencies:{CustomerService}'大寫... – Hitmands

回答

0

最常見的原因屬性「GetCustomers的」依賴項是在終極版,可觀察到的是,它實際上是爲undefined提供undefined;通常是因爲import/require不正確。請仔細檢查您是否正確導入CustomerService,並將調試器暫停在文件中,並將其傳遞到createEpicMiddleware以確認它是(或不是)undefined

我看到的另一件事是他們正在使用還沒有支持依賴注入的舊版本的redux-observable。 但是我非常有信心這是上面的第一個建議,因爲在默認下,redux-observable沒有提供任何東西作爲你的史詩的第三個參數。因此,在你的情況下,如果可指責的是可重複的,你實際上會得到Cannot read property 'customerService' of undefined,因爲第三個參數不是什麼,而是它提供的對象。

+0

嘿ANKIT。是這個問題嗎? – jayphelps