@acandylevey/ngrx-http-tracking 中文文档教程

发布于 3年前 浏览 18 项目主页 更新于 3年前

ngrx-http-tracking

这个 NGRX 库旨在插入您现有的 NGRX 存储,以减少您需要编写的样板代码的数量,并使处理 HTTP 请求的加载、成功和错误状态变得更加容易。

安装

npm i @acandylevey/ngrx-http-tracking

导入模块

...
import { NgrxHttpTrackingModule } from '@acandylevey/ngrx-http-tracking';

 imports: [
    ...
    NgrxHttpTrackingModule,
 ]

创建一个 http-tracked 动作

export const fetchUsers = createTrackingActions<UserRequest, User[]>(
  'Users',
  'fetchUsers'
);

创建你的效果

import { UserApiService } from './user-api.service';
import * as UserActions from './user.actions';
...
fetchUsers$ = createTrackingEffect(
    this.actions$,
    UserActions.fetchUsers,
    this.userApi.fetchUsers,
    'Could not load users'
  );

你的 reducer 没有太大变化,只是你不再需要担心跟踪加载状态

const usersReducer = createReducer(
  initialState,
  on(UserActions.fetchUsers.loaded, (state, { payload }) =>
    userssAdapter.setAll(payload, { ...state })
  )
);

跟踪结果

默认情况下,所有动作都带有一个全局标签然后可以很容易地在全局级别拦截,以提供全局错误处理或全局加载微调器(如果有任何操作正在等待 http 请求)。

 isLoaded$ = this.httpTracker.isLoaded(UserActions.fetchUsers);
 isLoading$ = this.httpTracker.isLoading(UserActions.fetchUsers);

 isGloballyLoading$ = this.httpTrackingFacade.getGlobalLoading();
 globalErrors$ = this.httpTrackingFacade.getGlobalErrors();

您还可以将自己的标签应用于一组操作,然后返回汇总结果

export const fetchUsers = createTrackingActions<UserRequest, User[]>(
  'Users',
  'fetchUsers',
  true, // has global tag still
  ['user-profiles']
);

isTagLoading$ = this.httpTracker.isTagLoading('user-profiles');

还有一种提供的方法可以在 http 请求已解决(成功或失败)后继续执行一段逻辑

中触发请求

 fetchUsers() {
    this.store.dispatch(UserActions.fetchUsers.loading());
    return UserActions.fetchUsers;
  }

从外观跟踪 它

this.httpTrackingFacade
      .getResolved(this.userFacade.fetchUsers())
      .subscribe((result) => {
        console.log(`The result from the fetch users call was: {result}`);
      });

ngrx-http-tracking

This NGRX library is aims to slot into your pre-existing NGRX stores to reduce the amount of boiler plate code you need to write and to make handling the loading, success and error states of HTTP Requests significantly easier.

Installing

npm i @acandylevey/ngrx-http-tracking

Importing the module

...
import { NgrxHttpTrackingModule } from '@acandylevey/ngrx-http-tracking';

 imports: [
    ...
    NgrxHttpTrackingModule,
 ]

Creating a http-tracked action

export const fetchUsers = createTrackingActions<UserRequest, User[]>(
  'Users',
  'fetchUsers'
);

Creating your effect

import { UserApiService } from './user-api.service';
import * as UserActions from './user.actions';
...
fetchUsers$ = createTrackingEffect(
    this.actions$,
    UserActions.fetchUsers,
    this.userApi.fetchUsers,
    'Could not load users'
  );

Your reducer doesn't change much, except you no longer need to worry about keeping track of the load state

const usersReducer = createReducer(
  initialState,
  on(UserActions.fetchUsers.loaded, (state, { payload }) =>
    userssAdapter.setAll(payload, { ...state })
  )
);

Tracking the results

By default, all actions are tagged with a global tag that can then be easily intercepted a global level, to provide global error handling or a global loading spinner if any actions are waiting on http requests.

 isLoaded$ = this.httpTracker.isLoaded(UserActions.fetchUsers);
 isLoading$ = this.httpTracker.isLoading(UserActions.fetchUsers);

 isGloballyLoading$ = this.httpTrackingFacade.getGlobalLoading();
 globalErrors$ = this.httpTrackingFacade.getGlobalErrors();

You can also apply your own tags to a set of actions and then get summarised results back

export const fetchUsers = createTrackingActions<UserRequest, User[]>(
  'Users',
  'fetchUsers',
  true, // has global tag still
  ['user-profiles']
);

isTagLoading$ = this.httpTracker.isTagLoading('user-profiles');

There is also a provided way of simple continuing a peice of logic once a http request as resolved (Either success or failure)

Firing the reuqest from a facade

 fetchUsers() {
    this.store.dispatch(UserActions.fetchUsers.loading());
    return UserActions.fetchUsers;
  }

Tracking it

this.httpTrackingFacade
      .getResolved(this.userFacade.fetchUsers())
      .subscribe((result) => {
        console.log(`The result from the fetch users call was: {result}`);
      });
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文