Angular 在app.reducer.ts错误中检查角度NgRx类型
我有一个简单的登录/注销状态管理。 我得到这个错误: 类型'(状态:状态|未定义,操作:authActions)=>state'不可分配给类型'ActionReducer'。 参数“action”和“action”的类型不兼容。 类型“Action”不可分配给类型“authActions”。 类型“Action”不可分配给类型“Logout”。 属性“type”的类型不兼容。 类型“string”不可分配给类型“types.LOGOUT” 这是我的档案 auth.actions.tsAngular 在app.reducer.ts错误中检查角度NgRx类型,angular,typescript,ngrx,typescript2.0,Angular,Typescript,Ngrx,Typescript2.0,我有一个简单的登录/注销状态管理。 我得到这个错误: 类型'(状态:状态|未定义,操作:authActions)=>state'不可分配给类型'ActionReducer'。 参数“action”和“action”的类型不兼容。 类型“Action”不可分配给类型“authActions”。 类型“Action”不可分配给类型“Logout”。 属性“type”的类型不兼容。 类型“string”不可分配给类型“types.LOGOUT” 这是我的档案 auth.actions.ts impor
import { Action } from '@ngrx/store';
import { User } from '../user/user.model';
export enum types {
LOGIN = '[AUTH] LOGIN',
LOGOUT = '[AUTH] LOGOUT',
}
export class Login implements Action {
readonly type = types.LOGIN;
constructor(public payload: User) {}
}
export class Logout implements Action {
readonly type = types.LOGOUT;
}
export type authActions = Login | Logout;
auth.reducer.ts
import { User } from '../user/user.model';
import * as authActions from './auth.actions';
export interface State {
isLoggedIn: boolean;
user: User | null;
}
const initialState: State = {
isLoggedIn: false,
user: null,
};
export function authReducer(
state: State = initialState,
action: authActions.authActions
): State {
switch (action.type) {
case authActions.types.LOGIN:
return { ...state, isLoggedIn: true, user: action.payload };
case authActions.types.LOGOUT:
return { ...state, isLoggedIn: false, user: null };
default:
return state;
}
}
import { ActionReducerMap } from '@ngrx/store';
import * as fromAuthReducer from '../auth/store/auth.reducer';
export interface AppState {
auth: fromAuthReducer.State;
}
export const appReducer: ActionReducerMap<AppState> = {
auth: fromAuthReducer.authReducer,
};
app.ts
import { User } from '../user/user.model';
import * as authActions from './auth.actions';
export interface State {
isLoggedIn: boolean;
user: User | null;
}
const initialState: State = {
isLoggedIn: false,
user: null,
};
export function authReducer(
state: State = initialState,
action: authActions.authActions
): State {
switch (action.type) {
case authActions.types.LOGIN:
return { ...state, isLoggedIn: true, user: action.payload };
case authActions.types.LOGOUT:
return { ...state, isLoggedIn: false, user: null };
default:
return state;
}
}
import { ActionReducerMap } from '@ngrx/store';
import * as fromAuthReducer from '../auth/store/auth.reducer';
export interface AppState {
auth: fromAuthReducer.State;
}
export const appReducer: ActionReducerMap<AppState> = {
auth: fromAuthReducer.authReducer,
};
从'@ngrx/store'导入{ActionReducerMap};
从“../auth/store/auth.reducer”导入*作为AuthReducer;
导出接口AppState{
auth:fromAuthReducer.State;
}
导出常量程序:ActionReducerMap={
auth:fromAuthReducer.authReducer,
};
使用ngrx的旧语法是个问题。您应该使用更好地支持typescript的creator语法
代码的解决方案是使用ActionReducerMap的any
类型:
导出常量程序:ActionReducerMap={
auth:fromAuthReducer.authReducer,
};
使用ngrx的旧语法是个问题。您应该使用更好地支持typescript的creator语法
代码的解决方案是使用ActionReducerMap的any
类型:
导出常量程序:ActionReducerMap={
auth:fromAuthReducer.authReducer,
};
使用任何工具都会导致错误的自动完成吗?不在这里。如果您不想使用任何
,您应该使用createReducer/on而不是reducerokay的switch/case语法,谢谢您使用任何导致错误自动完成的原因?这里没有。如果您不想使用任何
,您应该使用createReducer/on而不是reducerokay的switch/case语法,谢谢