2017-04-18 36 views
3

我試圖將一個動態參數傳遞給重選選擇器。原因是這個參數實際上是一個未知的角度路由參數。它也不能成爲國家的一部分。創建一個採用動態參數的重選選擇器的問題

下面是從所經過的路由參數訂閱組件的相關代碼:

this.store.select(fromRoot.getMessagesWithOtherUserAccount(this.route.params['otherId'])) 
     .subscribe(messages => this.messagesWithOtherUserAccount = messages); 

下面是選擇代碼:

const getMessagesState = (state: State) => state.message.messages; 

//See error below... How can I pass my otherId argument here?? 
const messagesWithOtherUserAccount = createSelector(getMessagesState, messagesWithCounterParty); 

export const getMessagesWithOtherUserAccount = (otherId: number) => messagesWithOtherUserAccount(otherId); 

.... 
export const messagesWithCounterParty = (messages: Message[]) => (otherId: number) => withOtherUserAccount(otherId, messages); 

以下是錯誤我得到:

「數字」類型的參數不可分配給類型爲 'State'的參數。

我想在otherId參數的messagesWithOtherUserAccountcreateSelector通過,但我不知道如何...

是否有人可以幫忙嗎?

回答

1

我能想出以下解決方案:

this.store.select(fromRoot.getMessagesWithCounterParty(this.route.snapshot.params['otherId'])) 
    .subscribe(messages => this.messagesWithOtherUserAccount = messages); 

export const getMessagesWithCounterParty = (otherId: number) => createSelector(getMessagesState, (messages: Message[]) => withOtherUserAccount(otherId, messages)); 
0

createSelector可以創建能夠接受任意數量的自定義/動態參數的選擇!見createSelector API

在你的情況的僞代碼才達到你的結果可能是:你

// ... 

export const getMessagesWithCounterParty = createSelector(
    getMessagesState,    // Accepts the state as 1st argument 
    (otherId: number) => otherId, // Accepts an Id as 2nd argument 

    // Result function 
    (messages: Message[], otherId: number) => withOtherUserAccount(messages, otherId), 
); 

// Later in your application: 
getMessagesWithCounterParty(yourState, 42); 

PS.The錯誤是不是從你的應用程序,但是從你的類型檢查(可能打字稿)。

相關問題