使用 Angular 2 创建 SOAP 客户端

Cou*_*uim 5 soap soap-client typescript ionic-framework angular

我正在寻找一种使用 WSDL 向 Web 服务发送 SOAP 请求的方法。使用 Typescript 2 和 Angular 2 可以做到这一点吗?

我看过 Angular 1 的教程,但他们使用了旧的角度方法,例如factorycontroller

如果可能的话,我希望有一种使用 TypeScript 来实现这一点的新方法。

有任何想法吗 ?

Sup*_*miu 4

您需要的是一个包装Http并提供反序列化的服务:

@Injectable()
export class SOAPService{

    constructor(private http:Http){}

    public get(url:string, options?:RequestOptionsArgs):Observable<any>{
        return this.http.get(url, options).map(res => {
            let xmlresult = res.text();
            let result = //Here you deserialize your xml object
            return result;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以这样使用它:

@Component({...})
export class ExampleComponent{
    constructor(private soap:SOAPService){}


    foo():{
        this.soap.get('foo/bar').subscribe(...);
    }
}
Run Code Online (Sandbox Code Playgroud)

由于我不是 xml 解析专家,我无法告诉您如何反序列化 XML,但您可以检查MDN,只需将序列化/反序列化过程包装在另一个服务中并将其注入到您的SOAPService.