Angular 6简单的肥皂API调用



您是否可以从组件中调用Angular 6中的SOAP API调用?

如果是这样,您可以举一个简单呼叫和处理对象的返回的示例?

只要您可以使用http访问API,就可以使用Angular的HTTPClient库访问API。

举例来说,您可以从此处开始:https://angular.io/guide/http#httpclient

或我在这里有一个简单的stackblitz:https://stackblitz.com/edit/angular-simple-retrieve-deborahk

接口

// Define the shape of the incoming data
export interface Product {
  productId: number;
  productName: string;
  productCode: string;
}

服务

import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, tap, map } from 'rxjs/operators';
@Injectable({
  providedIn: 'root'
})
export class ProductService {
  // To try error handling, change this URL to product instead of products
  private productUrl = 'assets/products/products.json';
  constructor(private http: HttpClient) { }
  getProducts(): Observable<Product[]> {
    // Use Angular's HttpClient library to issue a GET request
    // OPTIONAL: Pipe it through operators for debugging, data
    // manipulation, or error handling
    return this.http.get<Product[]>(this.productUrl).pipe(
      tap(data => console.log('All: ' + JSON.stringify(data))),
      catchError(error => this.handleError<Product[]>(error, 'get', []))
    );
  }
  // To try out the error handling, change the Url above
  private handleError<T>(err: HttpErrorResponse, operation = 'operation', result?: T) {
    // in a real world app, we may send the server to some remote logging infrastructure
    // instead of just logging it to the console
    let errorMessage = '';
    if (err.error instanceof ErrorEvent) {
      // A client-side or network error occurred. Handle it accordingly.
      errorMessage = `An error occurred: ${err.error.message}`;
    } else {
      // The backend returned an unsuccessful response code.
      // The response body may contain clues as to what went wrong,
      errorMessage = `Server returned code: ${err.status}, error message is: ${err.message}`;
    }
    console.error(errorMessage);
    return throwError(errorMessage);
  }
}

最新更新