Angular: 使用ngrx进行嵌套的http调用
创始人
2024-10-23 00:01:22
0

在Angular中使用ngrx进行嵌套的HTTP调用,您可以按照以下步骤进行解决:

  1. 首先,确保您已经安装了ngrx/store和ngrx/effects。您可以使用以下命令进行安装:
npm install @ngrx/store @ngrx/effects
  1. 创建一个名为user.actions.ts的文件,用于定义用户相关的动作。在该文件中,定义一个名为LoadUser的动作,用于加载用户数据。
import { Action } from '@ngrx/store';

export enum UserActionTypes {
  LoadUser = '[User] Load User',
  UserLoaded = '[User] User Loaded',
  UserLoadFailed = '[User] User Load Failed'
}

export class LoadUser implements Action {
  readonly type = UserActionTypes.LoadUser;

  constructor(public payload: { userId: number }) {}
}

export class UserLoaded implements Action {
  readonly type = UserActionTypes.UserLoaded;

  constructor(public payload: { user: any }) {}
}

export class UserLoadFailed implements Action {
  readonly type = UserActionTypes.UserLoadFailed;

  constructor(public payload: { error: any }) {}
}

export type UserActions = LoadUser | UserLoaded | UserLoadFailed;
  1. 创建一个名为user.effects.ts的文件,用于处理用户相关的副作用。在该文件中,定义一个名为loadUser$的效果,用于处理LoadUser动作,并发出UserLoadedUserLoadFailed动作。
import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { of } from 'rxjs';
import { switchMap, map, catchError } from 'rxjs/operators';

import { UserService } from './user.service';
import { LoadUser, UserLoaded, UserLoadFailed, UserActionTypes } from './user.actions';

@Injectable()
export class UserEffects {
  constructor(private actions$: Actions, private userService: UserService) {}

  @Effect()
  loadUser$ = this.actions$.pipe(
    ofType(UserActionTypes.LoadUser),
    switchMap(action =>
      this.userService.getUser(action.payload.userId).pipe(
        map(user => new UserLoaded({ user })),
        catchError(error => of(new UserLoadFailed({ error })))
      )
    )
  );
}
  1. 创建一个名为user.service.ts的文件,用于处理用户相关的HTTP请求。在该文件中,定义一个名为getUser的方法,用于获取用户数据。
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable()
export class UserService {
  constructor(private http: HttpClient) {}

  getUser(userId: number): Observable {
    return this.http.get(`/api/users/${userId}`);
  }
}
  1. 在您的模块中注册ngrx/store和ngrx/effects。
import { NgModule } from '@angular/core';
import { StoreModule } from '@ngrx/store';
import { EffectsModule } from '@ngrx/effects';

import { userReducer } from './user.reducer';
import { UserEffects } from './user.effects';

@NgModule({
  imports: [
    StoreModule.forFeature('user', userReducer),
    EffectsModule.forFeature([UserEffects])
  ]
})
export class UserModule {}
  1. 在您的组件中,使用ngrx/store来调度LoadUser动作,并通过ngrx/effects来处理加载用户的副作用。
import { Component } from '@angular/core';
import { Store } from '@ngrx/store';

import { LoadUser } from './user.actions';

@Component({
  selector: 'app-user',
  template: `
    
  `
})
export class UserComponent {
  constructor(private store: Store) {}

  loadUser() {
    this.store.dispatch(new LoadUser({ userId: 1 }));
  }
}

通过上述步骤,您可以在Angular中使用ngrx进行嵌套的HTTP调用。当您点击"Load User"按钮时,将触发LoadUser动作,并通过ngrx/effects处理副作用,从而加载用户数据。

相关内容

热门资讯

银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...