[英]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.