【发布时间】:2020-07-03 04:41:33
【问题描述】:
在我的 Angular 应用中;我习惯于从静态 json 文件中获取数据;
像这样:
@Injectable()
export class ConfigService {
constructor(private http: HttpClient) { }
getData() {
this.http.get('/assets/myfile.json').subscribe(data => {
console.log(data);
})
}
}
在某些场景中,myfile.json 可能不存在
(这是其他用途所需要的)
在这种情况下我不想得到这个错误:
HttpErrorResponse : 解析期间的 Http 失败 https://localhost:4201/assets/myfile.json
但很简单,我想得到一个null response
建议??
【问题讨论】:
-
您可以使用
catchError()并返回of(null)或根据某些情况重新抛出错误。
标签: angular typescript ecmascript-6 rxjs