2017-04-18 61 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错误是不是从你的应用程序,但是从你的类型检查(可能打字稿)。

相关问题