Angular 错误:引用错误:未定义ErrorEvent
当我试图从我的Angular 错误:引用错误:未定义ErrorEvent,angular,nativescript,Angular,Nativescript,当我试图从我的json服务器 baseURL已定义且可访问(如通过我的浏览器),因此在这方面没有问题 欢迎任何建议 dish.service.ts中的代码 import { Injectable } from '@angular/core'; import { Dish } from '../shared/dish'; import { Observable } from 'rxjs'; import { map, catchError } from 'rxjs/operators'; imp
json服务器
baseURL
已定义且可访问(如通过我的浏览器),因此在这方面没有问题
欢迎任何建议
dish.service.ts中的代码
import { Injectable } from '@angular/core';
import { Dish } from '../shared/dish';
import { Observable } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { baseURL } from '../shared/baseURL';
import { ProcessHTTPMsgService } from './process-httpmsg.service';
@Injectable({
providedIn: 'root'
})
export class DishService {
constructor(private http: HttpClient,
private processHTTPMsgService: ProcessHTTPMsgService) { }
getDishes(): Observable<Dish[]> {
return this.http.get<Dish[]>(baseURL + 'dishes')
.pipe(catchError(this.processHTTPMsgService.handleError));
}
getDish(id: string): Observable<Dish> {
return this.http.get<Dish>(baseURL + 'dishes/' + id)
.pipe(catchError(this.processHTTPMsgService.handleError));
}
getFeaturedDish(): Observable<Dish> {
return this.http.get<Dish[]>(baseURL + 'dishes?featured=true').pipe(map(dishes => dishes[0]))
.pipe(catchError(this.processHTTPMsgService.handleError));
不是答案,但这些信息可能会有所帮助 我遇到了类似的问题,IE9中的ErrorEvent引用错误 经过调查,我发现ErrorEvent是IE9中不支持的API(Javascript) ErrorEventAPI: 我在这里寻找答案
您没有导入任何名为ErrorEvent的内容,您希望它来自哪里?这不是Node/JavaScript的本地对象ErrorEvent。我认为Alexander错了,因为它是一个js对象。我正在试图找出问题所在,因为你的代码看起来很完美,我不明白。如果你能提供整个erros消息,那将很有帮助(折叠,没有扩展,太长,没有用)根据我,你想要/应该构建什么,是一个基于HTTPInterceptor的解决方案,它可以捕获来自任何http/https调用的错误,您还可以决定如何管理这些错误。正如@jcuypers已经提到的,您必须尝试使用一个拦截器,这是一种理想的方法。您面临此问题的平台是什么?您正在测试的设备上的操作系统版本是什么?端点是否安全(https)?您有问题吗?
import { Injectable } from '@angular/core';
import { throwError } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class ProcessHTTPMsgService {
constructor() { }
public handleError(error: HttpErrorResponse | any) {
let errMsg: string;
if (error.error instanceof ErrorEvent) {
errMsg = error.error.message;
console.log('errMsg (is instanceof ErrorEvent): ', errMsg)
} else {
errMsg = `${error.status} - ${error.statusText || ''} ${error.message}`;
console.log('errMsg (is NOT instanceof ErrorEvent): ', errMsg)
}
return throwError(errMsg);
}
}