首页 > 解决方案 > 类型承诺上不存在属性映射

问题描述

在我的 Angular 6 项目中,即使我包括在内,类型承诺错误上也不存在属性映射

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/toPromise';

以下是我的代码

 submit(): Promise<any> {
        return http.post('api/SubmitEmployee', JSON.stringify(this.formData))
            .map((response: Response) => <any>response.json())
            .toPromise()
            .catch(this.handlePromiseError)
    }

    handlePromiseError(error: Response) {
        console.log(error)
    }

经过一些谷歌后,我找到了如下解决方案,但这也不起作用

import { map } from 'rxjs/operators';  

pipe(map((response: Response) => response.json()))

包含此内容后,它显示错误属性类型在类型 promise 上不存在 以下是我的自定义post方法

post(url: string, content: any, resolve: ResolveFunction = null, reject: RejectFunction = null) {
    this.clearCache();
    const retryFunc = _ => this.post(url, content, resolve, reject);
    return fetch(appSettings.baseApiUri + url, {
      method: 'POST',
      headers: this.getCustomHeader(),
      body: content
    })
      .then(result => {
        this.handleResponse(url, result, retryFunc, resolve, reject);
      })
      .catch(error =>
        this.debounce(_ => this.handleError(url, error, retryFunc).catch(_ => reject(error)), this.debounceTimeout)
      );
  }

标签: angularrxjsangular6rxjs6

解决方案


使用from来包装 http.post 就像 from(http.post().then()) 一样,它返回一个 observable 以便你可以管道流


推荐阅读