@PatriotSY

Почему не виден сервис?

Хочу сделать eventHandler для сервиса:

..
import { ToastrService } from 'ngx-toastr';
...
export class AuthService {
    constructor(
        protected toastr: ToastrService,
        protected http: HttpClient
    ) {
    }

    signUp(data): Observable<HttpResponse<string>> {
        const url = `${this.url}/sign-up`;
        return this.http.post<HttpResponse<string>>(url, data, httpOptions).pipe(
            catchError(this.handleError)
        );
    }

    protected handleError(error: HttpErrorResponse) {
        if (error.error instanceof ErrorEvent) {
            // A client-side or network error occurred. Handle it accordingly.
            console.error('An error occurred:', error.error.message);
        } else {
            // The backend returned an unsuccessful response code.
            // The response body may contain clues as to what went wrong,
            console.error(
                `Backend returned code ${error.status}, ` +
                `body was: ${error.error}`);
            this.toastr.error('Test', 'It\s something testing text');
        }
        // return an observable with a user-facing error message
        return throwError(
            'Something bad happened; please try again later.');
    }

}


Хандлер срабатывает, выводит в консоль ошибки, но вот в хендлере не работает тоаст. Пишет, что не может вызывать error у null объекта. Почему хендлер не видит ToastrService ? его как-то по другому нужно прокидывать в хендлер?
  • Вопрос задан
  • 63 просмотра
Решения вопроса 1
catchError(this.handleError)
У вас контекст теряется.

Можно сделать так:
catchError(error => this.handleError(error))
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы