如果在Angular 7中错误通知不显示,可能是由于错误处理逻辑的问题。以下是一种可能的解决方法,包含代码示例:
{{errorMsg}}
export class MyComponent {
errorMsg: string;
showErrorNotification(error: any) {
this.errorMsg = '发生了一个错误:' + error.message;
}
// 其他组件代码...
}
catchError
运算符处理错误,并调用错误处理方法显示错误通知。例如:import { catchError } from 'rxjs/operators';
import { throwError } from 'rxjs';
@Injectable()
export class MyService {
constructor(private http: HttpClient) {}
getData() {
return this.http.get('api/data')
.pipe(
catchError((error) => {
this.showErrorNotification(error);
return throwError(error);
})
);
}
// 其他服务代码...
}
通过这种方法,当发生错误时,错误通知将显示在组件模板中的相应位置。你可以根据需要自定义错误通知的样式和行为。