简体   繁体   English

Angular 7中的类型'OperatorFunction <{},{}>'错误上不存在属性'subscribe'

[英]Property 'subscribe' does not exist on type 'OperatorFunction<{}, {}>' error in Angular 7

while learning how to handle errors in Angular Typescript threw an error 在学习如何处理Angular Typescript中的错误时抛出错误

Property 'subscribe' does not exist on type 'OperatorFunction<{}, {}>' in this code block in my component 我组件中此代码块中的类型'OperatorFunction <{},{}>'上不存在属性'subscribe'

createPost(post: HTMLInputElement) {
this.service.newPost(post).subscribe(
  response => {
    this.posts.splice(0, 0, post);
  },
  (error: AppError) => {
    if (error instanceof BadInput) {
      // Set form errors and display them next to input fields
      this.form.setErrors(error.originalError);
    } else {
      alert("An unexpected error occurred.");
      console.log(error);
    }
  }
);
}

The service code returning the observable used by the component code is 返回组件代码使用的observable的服务代码是

newPost(post) {
return (
  this.http.post(this.url, post),
  catchError((error: Response) =>
    error.status === 400
      ? Observable.throw(new BadInput(error))
      : Observable.throw(new AppError(error))
  )
);
}

I need help resolving the error and an explanation on why the error is thrown or how best to achieve handling errors. 我需要解决错误的帮助,并需要解释为什么引发错误或如何最好地实现错误处理。

You should use pipe operator followed by a map operator followed by catchError operator. 您应该先使用管道运算符,再使用地图运算符,再使用catchError运算符。 You will have to import the map operator and the catchError operator from rxjs/operators. 您将必须从rxjs / operators导入map运算符和catchError运算符。 Please see below: 请看下面:

.pipe(
    mergeMap(resp => {
        // add logic
    }),
    catchError(err => {
        // add handling 
    })
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:类型&#39;OperatorFunction &lt;{},{} |属性&#39;subscribe&#39;不存在 可观察的 <any> &gt; - Error: property 'subscribe' does not exist on type 'OperatorFunction<{}, {} | Observable<any>> “OperatorFunction”类型上不存在属性“订阅”<response, {}> '</response,> - Property 'subscribe' does not exist on type 'OperatorFunction<Response, {}>' 在“OperatorFunction”类型上不存在获取属性“过滤器”<any, any> ' Angular 中的错误</any,> - Getting a Property 'filter' does not exist on type 'OperatorFunction<any, any>' error in Angular 角度2中的类型&#39;void&#39;上不存在属性&#39;subscribe&#39; - Property 'subscribe' does not exist on type 'void' in angular 2 如何解决 Angular 订阅错误? 类型“AngularFireList”上不存在属性“订阅”<unknown> &#39;.ts(2339) - How to solve Angular subscribe error? Property 'subscribe' does not exist on type 'AngularFireList<unknown>'.ts(2339) Angular2 map - &gt; subscribe数据类型错误(类型&#39;ErrorObservable&#39;上不存在属性&#39;length&#39;。) - Angular2 map -> subscribe data type error ( Property 'length' does not exist on type 'ErrorObservable'.) 类型 {} 上不存在属性“订阅” - Property 'subscribe' does not exist on type {} Angular 2属性订阅在Promise类型上不存在<any> - Angular 2 Property subscribe does not exist on type Promise <any> 在Angular 2 App中返回一个Observable,但仍然出现错误:“ void”类型不存在“属性“ subscribe” - Returning an Observable in Angular 2 App, but still getting error: "Property 'subscribe' does not exist on type 'void' &#39;Promise&#39; 类型不存在属性 &#39;subscribe&#39; - Property 'subscribe' does not exist on type 'Promise'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM