[英]how to call get method of httpclient in angular 4 to retrieve data from server?
I am trying to get data from server using ngrx
. 我正在尝试使用
ngrx
从服务器获取数据。 I want to get data and save in store. 我想获取数据并保存在商店中。 I am using
ngrx/effect
. 我正在使用
ngrx/effect
。 I make the reduces, action , @effect file. 我制作reduces,@effect文件。 Now I want to know to call this function so that i will get data and save in my application state
现在我想知道调用此函数,以便我将获取数据并保存在我的应用程序状态中
here is my code https://stackblitz.com/edit/angular-kewril?file=src%2Fapp%2Fapp.module.ts 这是我的代码https://stackblitz.com/edit/angular-kewril?file=src%2Fapp%2Fapp.module.ts
Action 行动
import {Action} from '@ngrx/store';
export const FETCH_BANKLIST = 'FETCH_BANKLIST';
export class FetchRecipe implements Action {
readonly type = FETCH_BANKLIST;
}
export type BankAction = FetchRecipe;
Reducer 减速器
import * as bankActions from './bank.action';
export interface Bank {
seo_val: string;
text_val: string;
}
export interface State {
isLoading: boolean;
isLoadSuccess: boolean;
banks: Array<Bank>;
}
export const initialState: State = {
isLoading: false,
isLoadSuccess: false,
banks: []
};
export function BankListReducer(state = initialState, action: bankActions.BankAction) {
return state;
}
Ok, first you should create an effect file and include in EffectsModule in your 好的,首先您应该创建一个效果文件,并将其包含在自己的EffectsModule中
app.module.ts
file and here the code for creating an effect for your reference. app.module.ts
文件以及此处创建效果的代码供您参考。
@Effect()
getData$: Observable<Action|any> = this.actions$
.ofType(actions. FETCH_BANKLIST)
.map((action:actions. FetchRecipe) => action.payload)
.switchMap(payload => this.apiServices.fetchRecipies(payload)
.map(result => {
return new actions.SaveRecipieDetails(result);
})
.catch(e => {
return new actions.ApiError(e);
})
);
So, whenever your FETCH_BANKLIST is dispatched your effects gets triggered and it makes an API call using the services. 因此,无论何时调度FETCH_BANKLIST,您的效果都会被触发,并使用服务进行API调用。
app.services.ts
fetchRecipies(payload) {
const requestUrl = environment.apiUrl;
return this.http.post(requestUrl, payload);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.