简体   繁体   English

如何在角度4中调用httpclient的get方法从服务器检索数据?

[英]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.

相关问题 Javascript / Angular httpClient从url获取数据并通过外部方法 - Javascript / Angular httpClient get data from url and pass outside method 如何在角度控制器中检索从节点服务器发送的ejs数据? - How to retrieve ejs data sent from node server in an angular controller? 如果可用,如何创建角度服务以从 localstorage 获取数据,否则调用 api 方法 - How to create angular service to get data from localstorage if available otherwise call api method 使用 Angular httpClient 获取用户数据 - Get user data with Angular httpClient 如何从服务器获取数据并使用钩子在多个组件中检索它? - How to get data from server and use a hook to retrieve it in multiple components? Angular 4.3.3 HttpClient:如何从响应的 header 中获取值? - Angular 4.3.3 HttpClient : How get value from the header of a response? 如何使用 httpClient 从 angular 中的 observables 访问数据? - How to access data from observables in angular using httpClient? 如何在 Angular 中的 httpClient 调用中产生错误? - How to generaet error in httpClient call in Angular? 如何从对sqlite3.Database.all方法的调用中检索数据? - How can I retrieve data from a call to sqlite3.Database.all method? 如何从 Angular 的 SQLite 数据库中检索数据? - How to retrieve data from a SQLite db in Angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM