我定义了其他类使用的类( ng类ErrorService ),在该类中,我执行了两项操作,但未运行。 对于错误400,它进入第一个条件,但页面地址未更改;对于错误401,它进入第二个条件,但(对话框)没有打开,我该如何解决该问题?
ErrorService:
import { MatDialog } from '@angular/material/dialog';
import { Router } from '@angular/router';
import { HttpErrorResponse } from '@angular/common/http';
import { MessageSuccessfulComponent } from '../dialog-show-message/message-successful/message-successful.component';
import { throwError } from 'rxjs';
export class ErrorService {
constructor() {
}
public dialog: MatDialog
private router: Router;
showErroe(ErrorResponse: HttpErrorResponse) {
console.log(ErrorResponse.error);
if (ErrorResponse.status == 401) {
localStorage.removeItem('token')
this.router.navigate(['/ali']);
} else if (ErrorResponse.status == 400) {
const dialogRef = this.dialog.open(MessageSuccessfulComponent, {
width: '350px',
});
}
return throwError('plase again')
}
}
RegisteredUsersService:
import { ErrorService } from 'src/app/service/error-service';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { catchError } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class RegisteredUsersService {
constructor(private http: HttpClient) { }
private test2=new ErrorService();
postEditPassword(pass) {
const url ="https://localhost:11111/api/Account/ChangePassword";
return this.http.post(url, pass)
.pipe(catchError(this.test2.showErroe));
}
}
控制台(如果错误400):
POST https://localhost:11111/api/Account/ChangePassword 400
TypeError: Cannot read property 'open' of undefined
at CatchSubscriber.showErroe [as selector] (error-service.ts:18)
at CatchSubscriber.error (catchError.js:29)
at MapSubscriber._error (Subscriber.js:75)
at MapSubscriber.error (Subscriber.js:55)
at FilterSubscriber._error (Subscriber.js:75)
at FilterSubscriber.error (Subscriber.js:55)
at MergeMapSubscriber.notifyError (OuterSubscriber.js:7)
at InnerSubscriber._error (InnerSubscriber.js:14)
at InnerSubscriber.error (Subscriber.js:55)
at XMLHttpRequest.onLoad (http.js:2511)
控制台(如果出现错误401):
POST https://localhost:44354/api/Account/ChangePassword 401
TypeError: Cannot read property 'navigate' of undefined
at CatchSubscriber.showErroe [as selector] (error-service.ts:16)
at CatchSubscriber.error (catchError.js:29)
at MapSubscriber._error (Subscriber.js:75)
at MapSubscriber.error (Subscriber.js:55)
at FilterSubscriber._error (Subscriber.js:75)
at FilterSubscriber.error (Subscriber.js:55)
at MergeMapSubscriber.notifyError (OuterSubscriber.js:7)
at InnerSubscriber._error (InnerSubscriber.js:14)
at InnerSubscriber.error (Subscriber.js:55)
at XMLHttpRequest.onLoad (http.js:2511)
使用(ng v)命令所需的信息。
Angular CLI: 9.1.7
Node: 14.3.0
OS: win32 x64
Angular: 9.0.7
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.900.7
@angular-devkit/build-angular 0.900.7
@angular-devkit/build-optimizer 0.900.7
@angular-devkit/build-webpack 0.900.7
@angular-devkit/core 9.0.7
@angular-devkit/schematics 9.1.7
@angular/cdk 9.2.1
@angular/cli 9.1.7
@angular/localize 9.1.11
@angular/material 9.2.1
@ngtools/webpack 9.0.7
@schematics/angular 9.1.7
@schematics/update 0.901.7
rxjs 6.5.4
typescript 3.7.5
webpack 4.41.2