简体   繁体   English

如何使用服务在角度6中消除错误?

[英]how to tslint error in angular 6 using service?

I make a service and used that service in component .mY code is working fine .But I am getting tslint error why ? 我提供了一个服务,并在组件.mY代码中使用了该服务,但工作正常。但是我却收到tslint错误,为什么? function on which I am getting error 我遇到错误的功能

getUsers(): Observable<HttpResponse<UserModel>> {
    return this.http.get<UserModel>(this.configUrl).pipe(
      catchError(this.handleError)
    );
  }

code https://stackblitz.com/edit/angular-4pkct8?file=src%2Fapp%2Fuserdetail.service.ts 代码 https://stackblitz.com/edit/angular-4pkct8?file=src%2Fapp%2Fuserdetail.service.ts

在此处输入图片说明

在此处输入图片说明

You need to typecast response like this - 您需要像这样键入响应-

getUsers(): Observable<HttpResponse<UserModel>> {
return this.http.get<HttpResponse<UserModel>>(this.configUrl).pipe(
  catchError(this.handleError)
 );
}

You are type casting your http.get to <UserModel> , but your method expects you to return Observable<HttpResponse<UserModel>> . 您正在将http.get类型转换为<UserModel> ,但是您的方法希望您返回Observable<HttpResponse<UserModel>>

Change to method signatures return value to Observable<UserModel> so they match. 更改为方法签名后, Observable<UserModel>值返回给Observable<UserModel>以便它们匹配。

getUsers(): Observable<UserModel> {
  return this.http.get<UserModel>(this.configUrl).pipe(
    catchError(this.handleError)
  );
}

Your error is because "throwerror()" returns an Observable<never> 您的错误是因为“ throwerror()”返回一个Observable<never>

You are returning two different types one on success ( Observable<UserModel> ) and one on error ( Observable<never> ). 您将返回两种不同的类型,一种返回成功( Observable<UserModel> ),另一种返回错误( Observable<never> )。

The below would stop your error showing albeit maybe not having the result you want 下面可能会停止显示错误,尽管可能没有您想要的结果

getUsers(): Observable<HttpResponse<UserModel>> | Observable<never> {
   return this.http.get<HttpResponse<UserModel>>(this.configUrl).pipe(
     catchError(this.handleError)
);

} }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM