从服务器获取ngrx async数据
如何从服务器中获取异步数据?
我想将数据存储在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
还原仅用于纯(和同步)方法。
副作用,例如API调用,属于 @ngrx/效果。
通常的流量是:
请查看 数据。 NGRX的示例应用程序具有示例。
更多链接:
Reducers are only meant for pure (and synchronous) methods.
Side effects, like an API call, belong in @ngrx/effects.
The usual flow is:
Take a look at the example app of ngrx to have an example.
More links: