输入'可观察的<从不> | undefined' 不可分配给类型 'ObservableInput<any>'

时间:2021-01-11 21:50:16

标签: angular typescript

谁能帮我解决这个问题?我正在努力使它像这样https://github.com/TryCatchLearn/DatingApp

ng --version

Angular CLI:11.0.5

节点:14.9.0

操作系统:win32 x64

dotnet --version

5.0.101

import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HTTP_INTERCEPTORS } from '@angular/common/http';
    import { Injectable } from '@angular/core';
    import { Observable, throwError } from 'rxjs';
    import { catchError } from 'rxjs/operators';
    
    @Injectable()
    
    export class ErrorInterceptor implements HttpInterceptor {
    
        intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
            return next.handle(req).pipe(
                catchError(error => {
                    if (error instanceof HttpErrorResponse) {
                        if (error.status == 401) {
                            return throwError(error.statusText);
                        }
                        const applicationError = error.headers.get('Application-Error');
                        if (applicationError) {
                            console.log(applicationError);
                            return throwError(applicationError);
                        }
    
                        const serverError = error.error;
                        let modalStateErrors = '';
                        if (serverError && typeof serverError === 'object') {
                            for (const key in serverError) {
                                if (serverError[key]) {
                                    modalStateErrors += serverError[key] = '\n';
                                }
                            }
                        }
                        return throwError(modalStateErrors || serverError || 'Server Error');
                    }
                })
            );
        }
    
    }
    
    export const ErrorInterceptorProvider = {
        provide: HTTP_INTERCEPTORS,
        useClass: ErrorInterceptor,
        multi: true
    };

0 个答案:

没有答案