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);

这是我得到的错误:

“数字”类型的参数不能分配给“状态”类型的参数。

我想将otherId论点传递给messagesWithOtherUserAccount createSelector,但我不确定如何...

有人可以帮忙吗?

4

2 回答 2

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));
于 2017-04-23T18:28:27.863 回答
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.您得到的错误不是来自您的应用程序,而是来自您的类型检查器(可能是 Typescript)。

于 2017-04-25T21:34:43.073 回答