ngrx store将Reducer throw错误组合为错误TS2322:类型'ActionReducer'不可分配

问题描述 投票:0回答:1

我正在使用ngrx,并尝试组合2个减速器。得到错误

ERROR in src/app/state/index.ts(17,7): error TS2322: Type 'ActionReducer<{}, Action>' is not assignable to type 'ActionReducer<AppState, Action>'.
  Type '{}' is missing the following properties from type 'AppState': count, title

无法理解。怎么解决这个问题?

这是我的代码:我在哪里得到错误。

import { combineReducers, ActionReducer } from '@ngrx/store';
import { compose } from '@ngrx/core/compose';
import { storeFreeze } from 'ngrx-store-freeze';


import { reducerCount } from "./reducerCount";
import { reducerTitle } from "./reducerTitle";
import { AppState } from "./../models/State";

const reducers = {
    redCount : reducerCount,
    redTitle : reducerTitle
}


const developmentReducer: ActionReducer<AppState> = compose(storeFreeze, combineReducers)(reducers); //throws error here

export function reducer(state: any, action: any) {
  return developmentReducer(state, action);
}

这是我的AppState:

import { TitleState } from "./TitleState";//has model
import { CounterState } from "./CouterState";//has model

export interface AppState {
    count : CounterState;
    title : TitleState;
}

TitleState.ts
    export interface CounterState {
        counter:number
    }
CouterState.ts    
    export interface TitleState {
        title:string;
    }

这是我的减速器:

    export function reducerCount(state=0, action) {
        switch (action.type) {
            case "INCREMENT":
                return state + 1;

            case "DECREMENT":
                return state - 1;

            default:
                return state;
        }
    }


const basicValues = {
    title:"ABC"
}

export function reducerTitle(state=basicValues, action) {
    switch (action.type) {
        case "UPDATE_TITLE":
            return {
                ...state,
                title:"TCH"
            }

        default:
            return state;
    }
}

Live Demo here =>请检查状态 - > index.ts

angular angular7 ngrx-store
1个回答
1
投票

你qazxsw poi形状与你在qazxsw poi中提供的国家和平不符。更换:

AppState

成:

combineReducers()

值得注意的是 - 当你设置export interface AppState { counter : CounterState; title : TitleState; } export interface AppState { countReducer : CounterState; titleReducer : TitleState; } 时,NGRX中的combineReducers()被隐式使用

© www.soinside.com 2019 - 2024. All rights reserved.