如何从 typesafe redux-observable epic 中收听“@@router/LOCATION_CHANGE”动作

Posted

技术标签:

【中文标题】如何从 typesafe redux-observable epic 中收听“@@router/LOCATION_CHANGE”动作【英文标题】:How do I listen to '@@router/LOCATION_CHANGE' action from typesafe redux-observable epic 【发布时间】:2021-10-08 11:22:37 【问题描述】:

我正在尝试收听来自 typesafe redux-observable epic 的 '@@router/LOCATION_CHANGE' 动作,但我只是不明白如何。

    我需要过滤的具体操作是什么?我尝试'filter(onLocationChanged)' 失败。 什么是正确的'IN' 类型?使用 'LocationChangeAction''filter(onLocationChanged)' 我得到类型错误:

没有重载匹配这个调用。

    最后,如何通过'@@router/LOCATION_CHANGE' 操作更改位置? (之前和之后)
import  Epic  from "redux-observable";
import  of  from "rxjs";
import  filter, withLatestFrom, concatMap  from "rxjs/operators";
import  AppState  from "../../reducer";
import  CallHistoryMethodAction, LOCATION_CHANGE, push, onLocationChanged, RouterActionType, LocationChangeAction  from 'connected-react-router';


type IN = LocationChangeAction;
type OUT = ReturnType<typeof somthing>;

export const LocationListenEpic: Epic<IN | OUT, OUT, AppState> = (
    action$,
    state$
) => 
    action$.pipe(
        filter(onLocationChanged),
        withLatestFrom(state$),
        concatMap(([ payload , state]) => 

            console.log(currentLocation);
            return []

【问题讨论】:

【参考方案1】:

在深入了解 redux-observable epics 的工作原理后:


import  getLocation  from 'connected-react-router'

type IN = LocationChangeAction;
type OUT = ReturnType<typeof somthing>;

export const LocationListenEpic: Epic<IN | OUT, OUT, AppState> = (
    action$,
    state$
) => 
    action$.pipe(
        filter(action => action.type == LOCATION_CHANGE),
        withLatestFrom(state$),
        concatMap(([ payload , state]) => 

            console.log(getLocation(store.getState()));
            return []

【讨论】:

以上是关于如何从 typesafe redux-observable epic 中收听“@@router/LOCATION_CHANGE”动作的主要内容,如果未能解决你的问题,请参考以下文章

如何强制 Typesafe Activator 监听 0.0.0.0:8888

从 redux-observable 的史诗有条件地调度额外的动作

Redux-observable:当动作完成时组件如何订阅响应?

如何使用 TypeScript 为 Redux-Observable 的 Epic 输入 Actions

如何在 redux-observable 史诗中链接 RxJS 可观察?

如何在ngrx/effect(redux-observable)中调度多个动作?