간단한 알림 서비스를 설정했습니다.
@Injectable({
providedIn: 'root',
})
export class NotificationService {
constructor(private snackBar: MatSnackBar) {}
public openSnackbar(message: string, panelClass: string = 'success') {
this.snackBar.openFromComponent(SnackbarComponent, {
data: message,
panelClass: panelClass,
});
}
public handleError(response: any): void {
if (!response.error?.message) return;
this.snackBar.openFromComponent(SnackbarComponent, {
data: response.error?.message,
panelClass: 'warning',
});
}
}
이것은 내 스낵바 메시지를 설정하고 httpresponse 오류를 처리하는 데 사용됩니다. 다음과 같이 사용하면 :
.subscribe(
() => {
this.loading = false;
this.notificationService.openSnackbar(
'An email was sent to your email address. Please click the link and follow the instructions.'
);
this.router.navigate(['/login']);
},
(response: any) => this.notificationService.handleError(response)
);
모두 잘 작동합니다. 이 줄을 변경하고 싶습니다.
(response: any) => this.notificationService.handleError(response)
이에:
this.notificationService.handleError
그러나 그렇게 할 때 알림 서비스에 다음과 같은 오류가 발생합니다.
정의되지 않은 'openFromComponent'속성을 읽을 수 없습니다.
" 이 "는 더 이상 서비스가 아니기 때문이라고 생각합니다 . 어떻게 액세스 할 수 있습니다 이 나는 그것이 후자의 패턴을 변경하는 경우 방법에서?
컨텍스트를 바인딩해야합니다.
this.notificationService.handleError.bind(this.notificationService)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다