Angular Redux使用实体将数据检索到状态
创始人
2024-10-20 12:30:21
0

在Angular中使用Redux和实体将数据检索到状态的解决方法如下:

  1. 首先,确保已经安装了@ngrx/store@ngrx/entity库,可以使用以下命令进行安装:
npm install @ngrx/store @ngrx/entity
  1. 创建一个实体模型,可以是一个类或接口来表示实体的结构。例如,创建一个名为User的用户实体:
export interface User {
  id: number;
  name: string;
  email: string;
}
  1. 创建一个用户状态模型,它包含一个实体的映射,并定义一些其他属性,例如加载状态、错误信息等。例如,创建一个名为UserState的用户状态模型:
import { EntityState } from '@ngrx/entity';
import { User } from './user.model';

export interface UserState extends EntityState {
  loading: boolean;
  error: string | null;
}
  1. 创建一个用户存储器,使用createEntityAdapter创建一个实体适配器,用于管理用户实体的状态。在存储器中定义一些操作,例如加载用户、添加用户等。例如,创建一个名为UserStore的用户存储器:
import { createEntityAdapter, EntityState } from '@ngrx/entity';
import { createReducer, on } from '@ngrx/store';
import { User } from './user.model';
import * as UserActions from './user.actions';

export const userAdapter = createEntityAdapter();

export interface UserState extends EntityState {
  loading: boolean;
  error: string | null;
}

export const initialState: UserState = userAdapter.getInitialState({
  loading: false,
  error: null,
});

export const userReducer = createReducer(
  initialState,
  on(UserActions.loadUsers, (state) => ({
    ...state,
    loading: true,
    error: null,
  })),
  on(UserActions.loadUsersSuccess, (state, { users }) =>
    userAdapter.setAll(users, { ...state, loading: false })
  ),
  on(UserActions.loadUsersFailure, (state, { error }) => ({
    ...state,
    loading: false,
    error,
  })),
  // Add other action handlers here...
);

export function reducer(state: UserState | undefined, action: Action) {
  return userReducer(state, action);
}
  1. 在根模块中导入和配置存储器。在AppModule中导入StoreModuleStoreDevtoolsModule,并在imports数组中配置存储器和开发工具。例如:
import { StoreModule } from '@ngrx/store';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import { reducer } from './user.reducer';

@NgModule({
  imports: [
    StoreModule.forRoot({ users: reducer }),
    StoreDevtoolsModule.instrument(),
    // Other module imports...
  ],
})
export class AppModule {}
  1. 在组件中使用存储器。在需要使用用户数据的组件中,导入Store和相关的操作,例如加载用户、添加用户等。在组件中使用select操作符从存储器中选择用户状态,然后使用subscribe方法订阅状态的变化。例如:
import { Component, OnInit } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import { User } from './user.model';
import * as UserActions from './user.actions';

@Component({
  selector: 'app-user-list',
  template: `
    
  • {{ user.name }}
`, }) export class UserListComponent implements OnInit { users$: Observable; constructor(private store: Store<{ users: User[] }>) {} ngOnInit() { this.users$ = this.store.select((state) => state.users.entities); } loadUsers() { this.store.dispatch(UserActions.loadUsers()); } }

这样,当点击"Load Users"按钮时,将从服务器加载用户数据,并将其存储在用户状态中。组件将订阅用户状态的变化,并在列表中显示用户名称。

相关内容

热门资讯

科普攻略!德普之星辅助器app... 科普攻略!德普之星辅助器app,we poker辅助器,德州论坛(有挂软件)是一款可以让一直输的玩家...
重大科普!佛手在线大菠萝智能辅... 重大科普!佛手在线大菠萝智能辅助器,wepoker作弊辅助,分享教程(有挂软件);原来确实真的有挂(...
一分钟教会你!wepoker怎... 一分钟教会你!wepoker怎么增加运气,epoker透视,切实教程(有挂透视)1、点击下载安装,微...
六分钟了解!hhpoker有辅... 六分钟了解!hhpoker有辅助吗,wepoker国外版透视,扑克教程(有挂技巧)科技教程也叫必备教...
我来教大家!wepoker辅助... 我来教大家!wepoker辅助透视,wepoker免费脚本弱密码,详细教程(有挂透明);wepoke...
记者发布!wpk辅助,德普之星... 记者发布!wpk辅助,德普之星透视辅助软件激活码,解密教程(有挂辅助);亲真的是有正版授权,小编(透...
揭秘攻略!aapoker万能辅... 《揭秘攻略!aapoker万能辅助器,hhpoker真的假的,揭秘教程(有挂教程)》 aapoker...
重大通报!sohoo poke... 自定义sohoo poker辅助器系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用...
三分钟了解!wpk辅助器,hh... 1、三分钟了解!wpk辅助器,hhpoker免费辅助器,必赢教程(有挂神器);详细教程。2、hhpo...
玩家必看攻略!wejoker私... 玩家必看攻略!wejoker私人辅助软件,智星德州可以透视吗,透明挂教程(有挂技巧)关于智星德州可以...