Angular 6简单SOAP Api调用

问题描述 投票:-1回答:1

您是否能够从组件中调用Angular 6中的SOAP API调用?

如果是这样,你能举一个简单的调用和返回处理对象的例子吗?

angular typescript soap
1个回答
0
投票

只要您可以使用Http访问API,您就可以使用Angular使用Angular的HttpClient库来访问它。

例如,您可以从这里的文档开始: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);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.