Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Typescript:在类内调用静态方法(HttpInterceptor)_Angular_Typescript_Static_Static Methods_Angular Http Interceptors - Fatal编程技术网

Angular Typescript:在类内调用静态方法(HttpInterceptor)

Angular Typescript:在类内调用静态方法(HttpInterceptor),angular,typescript,static,static-methods,angular-http-interceptors,Angular,Typescript,Static,Static Methods,Angular Http Interceptors,我目前正在开发一个http拦截器,该拦截器直到昨天都运行良好 它定义了静态方法,其中一个不希望被识别 控制台显示: my.component.ts:162 PUT请求类型错误:HttpInterceptorService_1.HttpInterceptorService.createHttpErrorMessage不是函数 我的拦截器如下所示(我已将其简化为bug感兴趣的部分): //我的导入 @注射的({ providedIn:'根' }) 导出类HttpInterceptorService实

我目前正在开发一个http拦截器,该拦截器直到昨天都运行良好

它定义了静态方法,其中一个不希望被识别

控制台显示:

my.component.ts:162 PUT请求类型错误:HttpInterceptorService_1.HttpInterceptorService.createHttpErrorMessage不是函数

我的拦截器如下所示(我已将其简化为bug感兴趣的部分):

//我的导入
@注射的({
providedIn:'根'
})
导出类HttpInterceptorService实现HttpInterceptor{
//其他静态内容和我的httpInterceptorService下面
静态httpInterceptorService:httpInterceptorService;
建造师(
httpInterceptorService:httpInterceptorService,
) {
HttpInterceptorService.HttpInterceptorService=HttpInterceptorService;;
}
截取(req:HttpRequest,next:HttpHandler):可观察{
const headers=新的HttpHeaders({
//一些标题
});
const clone=req.clone({
// ..
});
返回next.handle(clone.pipe)(
// ..
);
}
createHttpErrorMessage(错误:HttpErrorResponse,状态文本:string){
const msg:string=error.status+''+error.statusText+'-'+statusText;
开关(错误状态){
案例404:
this.bathror('Error ID:'+this.ID,msg);
打破
违约:
打破
}
控制台错误(
//一些错误消息
);
}
handleHttpError(错误:HttpErrorResponse){
如果(我的情况){
//一些逻辑
}否则{
返回投掷器(错误)。管道(
retryWhen(errors=>errors.pipe(
延迟(1000),
以(1)为例,
点击(()=>{
开关(错误状态){
案例404://此方法不再被识别。。
HttpInterceptorService.HttpInterceptorService.createHttpErrorMessage(
错误,HttpInterceptorService.otherService.doSomething());
打破
违约:
控制台错误(error);
打破
}
}),
),
)
);
}
}
}
正如我所说的,到目前为止,拦截器工作没有任何问题,直到昨天出现这个错误


我做错了什么?

您可以进行以下更改之一,以修复编译问题

  • 使用

    this.createHttpErrorMessage(...)
    

  • 将该方法设为静态,并使用

    HttpInterceptorService.createHttpErrorMessage(...)
    

我注释“此方法不再被识别…”的部分将其更改为
This.createHttpErrorMessage(…)
或将该方法设置为静态,并使用
HttpInterceptorService.createHttpErrorMessage(…)调用它
++为什么您的服务类中确实需要静态引用?使用此.createHttpErrorMessage,控制台会说:\此.createHttpErrorMessage不是函数。它似乎可以使方法保持静态并将其移动到顶部。为了使它工作,我还必须使另一个方法成为静态的。我不得不将其设置为静态,因为该服务中还有很多其他内容,这是使其工作的唯一方法:(如果您愿意,可以将您的评论作为答案发布,让我确认一下:)
this.createHttpErrorMessage(...)
HttpInterceptorService.createHttpErrorMessage(...)