首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >类型'(state: State,action: AuthActionsUnion) => State‘的参数不可分配

类型'(state: State,action: AuthActionsUnion) => State‘的参数不可分配
EN

Stack Overflow用户
提问于 2019-08-23 00:44:56
回答 1查看 1.8K关注 0票数 0

我的main.module.ts.中一直有以下错误以下代码如下:

代码语言:javascript
复制
@NgModule({
  declarations: [
    PressComponent,
    LegalComponent,
    InviteComponent
  ],
  providers: [
    AuthService
  ],
  imports: [
    HomeModule,
    MainRoutingModule,
    ContactModule,
    EffectsModule.forRoot([
      AuthEffects
    ]),
    StoreModule.forRoot(AuthReducer, {
      metaReducers,
      runtimeChecks: {
        strictActionImmutability: true,
        strictStateImmutability: true,
      },
    }),
    SectionHeaderGroupModule,
    FormRegisterModule,
    ReactiveFormsModule
  ]
})

export class MainModule {
}

这是错误:

代码语言:javascript
复制
Error:(34, 25) TS2345: Argument of type '(state: State, action: AuthActionsUnion) => State' is not assignable to parameter of type 'ActionReducerMap<State, Action> | InjectionToken<ActionReducerMap<State, Action>>'.
  Type '(state: State, action: AuthActionsUnion) => State' is not assignable to type 'InjectionToken<ActionReducerMap<State, Action>>'.
    Property '_desc' is missing in type '(state: State, action: AuthActionsUnion) => State'.

这是我的auth.reducer.ts文件:

代码语言:javascript
复制
import { IUser } from '../shared/models/user';
import * as AuthActions from './auth.actions';

export interface State {
  user: IUser | null;
  error: any;
}

const initialState: State = {
  user: null,
  error: '',
};

export function AuthReducer(state = initialState, action: AuthActions.AuthActionsUnion): State {
  switch (action.type) {
    case AuthActions.AuthActionsTypes.GetUserDataGoogleSignIn: {
      return {
        ...state,
        user: action.payload,
        error: '',
      };
    }
    case AuthActions.AuthActionsTypes.SignIn: {
      return {
        ...state,
        error: '',
      };
    }
    case AuthActions.AuthActionsTypes.SignUp: {
      return {
        ...state,
        error: '',
      };
    }
    case AuthActions.AuthActionsTypes.GoogleSignIn: {
      return {
        ...state,
        error: '',
      };
    }
    case AuthActions.AuthActionsTypes.MobileGoogleSignIn: {
      return {
        ...state,
        error: '',
      };
    }
    case AuthActions.AuthActionsTypes.FacebookSignIn: {
      return {
        ...state,
        error: '',
      };
    }
    case AuthActions.AuthActionsTypes.MobileFacebookSignIn: {
      return {
        ...state,
        error: '',
      };
    }
    case AuthActions.AuthActionsTypes.SignInFailure: {
      return {
        ...state,
        user: null,
        error: getErrorMessage(action.payload),
      };
    }
    case AuthActions.AuthActionsTypes.SignUpFailure: {
      return {
        ...state,
        user: null,
        error: getErrorMessage(action.payload),
      };
    }
  }
}

function getErrorMessage(error): string {
  switch (error) {
    case 'auth/wrong-password':
      return 'Wrong password';
    case 'auth/user-not-found':
      return 'IUser does not exist';
    case 'auth/invalid-email':
      return 'Invalid email address';
    default:
      return error;
  }
}

我该怎么解决呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-23 03:59:39

我看到您混淆了ngrx v8和v7的语法,这里是我如何配置代码的。

在app.module.ts中

代码语言:javascript
复制
StoreModule.forRoot(ROOT_REDUCERS, {
  metaReducers,
  runtimeChecks: {
    strictStateImmutability: true,
    strictActionImmutability: true
  }
}),

然后是根减速器

代码语言:javascript
复制
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";

/**
 * Every reducer module's default export is the reducer function itself. In
 * addition, each module should export a type or interface that describes
 * the state of the reducer plus any selector functions. The `* as`
 * notation packages up all of the exports into a single object.
 */
/**
 * As mentioned, we treat each reducer like a table in a database. This means
 * our top level state interface is just a map of keys to inner state types.
 */
export interface State {
  router: fromRouter.RouterReducerState<any>;
}

/**
 * Our state is composed of a map of action reducer functions.
 * These reducer functions are called with each dispatched action
 * and the current or initial state and return a new immutable state.
 */
export const ROOT_REDUCERS = new InjectionToken<
  ActionReducerMap<State, Action>
>("Root reducers token", {
  factory: () => ({
    router: fromRouter.routerReducer
  })
});

以及ngrx v8写减速器的方式

代码语言:javascript
复制
import { createReducer, on } from "@ngrx/store";
import {
  EntityState,
  EntityAdapter,
  createEntityAdapter
} from "@ngrx/entity";

import * as PostActions from "../actions/post.actions";

import { IPost } from "../models/post.model";

export interface PostState extends EntityState<IPost> {
  selectedPost: IPost;
  errors: any;
}

const postAdapter: EntityAdapter<IPost> = createEntityAdapter<IPost>();

const initialState: PostState = postAdapter.getInitialState({
  selectedPost: null,
  errors: null
});

export const postReducer = createReducer(
  initialState,
  on(PostActions.LoadPostsSuccess, (state, { posts }) =>
    postAdapter.addAll(posts, state)
  ),
  on(PostActions.LoadPostSuccess, (state, { post }) => {
    return postAdapter.addOne(post, { ...state, selectedPost: post });
  }),
  on(PostActions.UpdatePost, (state, { post }) => {
    return postAdapter.updateOne(post, state);
    //return postAdapter.updateOne(post, { ...state, selectedPost: post });
  }),
  on(PostActions.UpdateSelectedPost, (state, { selectedPost }) => {
    console.log(selectedPost);
    return { ...state, selectedPost };
  }),

  //catch errors
  on(
    PostActions.LoadPostsFail,
    PostActions.LoadPostFail,
    PostActions.UpdatePostFail,
    (state, { errors }) => {
      return { ...state, errors };
    }
  )
);

export const { selectAll } = postAdapter.getSelectors();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57618543

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档