从服务器获取ngrx async数据

发布于 2025-01-27 11:20:07 字数 1765 浏览 4 评论 0原文

如何从服务器中获取异步数据?

我想将数据存储在Global Store中以进行

更新

以后

的 寻求帮助

app.components.ts:

   import { Component } from '@angular/core';
import { Store } from '@ngrx/store';
import { dataSelector, DATA_LOAD } from './store.sevice/ngrx.store/globalStore.action';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent {
  
  data$ = this.store.select(dataSelector)

  constructor(private store: Store) {

    this.store.dispatch(DATA_LOAD());

  }

reducer.reducer.ts:

import {ActionReducerMap, MetaReducer} from '@ngrx/store';
import { environment } from 'src/environments/environment';

import { counterReducer, DATA_KEY } from './globalStore.action';


export interface State {
  [DATA_KEY]: any;
}

export const reducers: ActionReducerMap<State> = {
  [DATA_KEY]: counterReducer,
};


export const metaReducers: MetaReducer<State>[] = !environment.production ? [] : [];

action.action.ts:

import {createAction, createFeatureSelector, createReducer, createSelector, on, props} from '@ngrx/store';

export const DATA_KEY = 'DATA';

export const DATA_LOAD = createAction('[DATA] DATA_LOAD');


export const initialState: any = {
  data: {}
};

export const counterReducer = createReducer(
  initialState,
  on(DATA_LOAD, state => ({
    ...state.data,
    ...fetch('https://gutendex.com/books')
        .then(response => response.json())
        .then(data => data)
  }))
);

export const featureSelector = createFeatureSelector<any>(DATA_KEY);

export const dataSelector = createSelector(
  featureSelector,
  state => state.data
);

how to get asynchronous data from the server?

i want to store data in global store for later update

but I can't understand the logic of osynchronous calls like in redux

with simple data, I was able to figure it out, but getting data from the server is not given to me

I really ask for help

app.components.ts:

   import { Component } from '@angular/core';
import { Store } from '@ngrx/store';
import { dataSelector, DATA_LOAD } from './store.sevice/ngrx.store/globalStore.action';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent {
  
  data$ = this.store.select(dataSelector)

  constructor(private store: Store) {

    this.store.dispatch(DATA_LOAD());

  }

reducer.reducer.ts:

import {ActionReducerMap, MetaReducer} from '@ngrx/store';
import { environment } from 'src/environments/environment';

import { counterReducer, DATA_KEY } from './globalStore.action';


export interface State {
  [DATA_KEY]: any;
}

export const reducers: ActionReducerMap<State> = {
  [DATA_KEY]: counterReducer,
};


export const metaReducers: MetaReducer<State>[] = !environment.production ? [] : [];

action.action.ts:

import {createAction, createFeatureSelector, createReducer, createSelector, on, props} from '@ngrx/store';

export const DATA_KEY = 'DATA';

export const DATA_LOAD = createAction('[DATA] DATA_LOAD');


export const initialState: any = {
  data: {}
};

export const counterReducer = createReducer(
  initialState,
  on(DATA_LOAD, state => ({
    ...state.data,
    ...fetch('https://gutendex.com/books')
        .then(response => response.json())
        .then(data => data)
  }))
);

export const featureSelector = createFeatureSelector<any>(DATA_KEY);

export const dataSelector = createSelector(
  featureSelector,
  state => state.data
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘然心甜 2025-02-03 11:20:07

还原仅用于纯(和同步)方法。
副作用,例如API调用,属于 @ngrx/效果。
通常的流量是:

  • 组件调度提取动作
  • 效果选择获取操作,进行API调用,并派遣取得成功或获取错误操作
  • 减少器可挑选Fetch成功,并且更新状态
  • 选择器是更新,并且组件获取数据,

请查看 数据。 NGRX的示例应用程序具有示例。

更多链接:

Reducers are only meant for pure (and synchronous) methods.
Side effects, like an API call, belong in @ngrx/effects.
The usual flow is:

  • component dispatch FETCH action
  • effect picks FETCH action up, makes the API call, and dispatches a FETCH SUCCESS or FETCH ERROR action
  • reducers picks up FETCH SUCCESS and updates states
  • selectors are updates and component gets the data

Take a look at the example app of ngrx to have an example.

More links:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文