BaseRouterStoreState缺少类型'ActionReducerMap <State,Action>'的属性

时间:2019-08-23 20:57:24

标签: angular typescript rxjs ngrx angular8

我一直收到以下错误:

  

错误:(11,18)TS2739:类型'{router:(state:RouterReducerState,action:Action)=> RouterReducerState; }'缺少以下内容   类型'ActionReducerMap'中的属性:用户,错误

我看过类似的示例:https://github.com/starjumper30/toh-ngrx8/blob/3a8d61d8423af8543de8a30aed16ee941ffcea0b/src/app/reducers/index.ts,它似乎以类似的方式设置并且运行良好。

这是我的代码:

import { Action, ActionReducer, ActionReducerMap, MetaReducer } from '@ngrx/store';
import { State } from '../modules/auth/auth.reducer';
import { environment } from '../../environments/environment';
import * as fromRouter from "@ngrx/router-store";
import { InjectionToken } from '@angular/core';

export interface State {
  router: fromRouter.RouterReducerState<any>;
}
export const ROOT_REDUCERS = new InjectionToken<ActionReducerMap<State, Action>>('Root reducers token', {
  factory: () => ({
    router: fromRouter.routerReducer
  })
});

export function logger(reducer: ActionReducer<State>): ActionReducer<State> {
  return (state: State, action: any): any => {
    const result = reducer(state, action);
    console.groupCollapsed(action.type);
    console.log('prev state', state);
    console.log('action', action);
    console.log('next state', result);
    console.groupEnd();
    return result;
  };
}

export const metaReducers: MetaReducer<State>[] = !environment.production
  ? [logger]
  : [];

此错误的原因是什么,我该如何解决?

1 个答案:

答案 0 :(得分:1)

我认为您这行有错误

import { State } from '../modules/auth/auth.reducer';

因此您可以删除它,因为文件中已经有一个State接口状态定义

在这里我也不使用State

return (state: State, action: any): any => { // remove State here

我的完整工作代码将是这样

import {
  ActionReducer,
  ActionReducerMap,
  MetaReducer,
  Action
} from "@ngrx/store";
import { environment } from "../../../environments/environment";
import * as fromRouter from "@ngrx/router-store";
import { InjectionToken } from "@angular/core";

export interface State {
  router: fromRouter.RouterReducerState<any>;
}

export const ROOT_REDUCERS = new InjectionToken<
  ActionReducerMap<State, Action>
>("Root reducers token", {
  factory: () => ({
    router: fromRouter.routerReducer
  })
});

export function logger(reducer: ActionReducer<State>): ActionReducer<State> {
  return (state, action) => {
    const result = reducer(state, action);
    console.groupCollapsed(action.type);
    console.log("prev state", state);
    console.log("action", action);
    console.log("next state", result);
    console.groupEnd();

    return result;
  };
}

export const metaReducers: MetaReducer<State>[] = !environment.production
  ? [logger]
  : [];