【问题标题】:Property 'subscribe' does not exist on type 'Promise<Client>'. ngx-soap package类型“Promise<Client>”上不存在属性“subscribe”。 ngx-soap 包
【发布时间】:2019-02-13 00:58:57
【问题描述】:

我正在尝试使用 ngx-soap 包通过 Angular 6 获取肥皂请求。 我有一个像这样处理 api 的服务:

import { Injectable } from '@angular/core';

@Injectable()
export class ApiService {

client: Client;

  constructor(
    private soap: NgxSoapService
  ) {
    this.soap.createClient('assets/wsdl/auth/auth.wsdl').subscribe(client => this.client = client);
  }
}

尽管这是包装页面上的确切示例,但我收到以下错误:

[ts] Property 'subscribe' does not exist on type 'Promise<Client>'.

我知道我可以使用 then 但不能使用这个包订阅吗?

【问题讨论】:

标签: angular soap


【解决方案1】:

您需要将 promise 转换为 observable,然后您才能订阅。

对于 RxJs v6,将您的代码更改为:

import { from } from 'rxjs'

const promise = this.soap.createClient('assets/wsdl/auth/auth.wsdl')
from(promise).subscribe(client => this.client = client)

对于 RxJs v5:

import 'rxjs/add/observable/fromPromise'
import { Observable } from 'rxjs/Observable'

const promise = this.soap.createClient('assets/wsdl/auth/auth.wsdl')
Observable.fromPromise(promise).subscribe(client => this.client = client)

【讨论】:

  • rxjs 没有从中导出元素。这样做有什么缺点吗?这似乎是一个奇怪的演员阵容
  • 你必须使用旧版本的 RxJS 你的 package.json 说的 rxjs 版本是什么?
  • "rxjs": "^5.5.6"
  • 为 v5 添加了答案
  • 这样做没有缺点 - 但如果可能的话,最好使用一个返回 observables 而不是 promises 的库
猜你喜欢
  • 2020-11-01
  • 2019-12-26
  • 2019-03-09
  • 2017-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-02
相关资源
最近更新 更多