[Angular] Handle HTTP Errors in Angular with HttpErrorResponse interface

When communicating with some backend API, data travels over the network using the HTTP protocol. As such, failures may occur, be it on our own device (i.e. the browser) or on the server-side which may not be available or unable to process our request. We need to handle such error responses and give the user a proper feedback.

import { HttpErrorResponset } from ‘@angular/common/http‘;

export class AppComponent {
  people;
  message;
  constructor(private peopleService: PeopleService) {}

  fetchPeople() {
    this.peopleService
      .fetchPeople()
      .subscribe(
        (data) => {
          this.message = null;
          this.people = data;
        },
        (err: HttpErrorResponse) => {
          if (err instanceof Error) {
            // client-side error
            this.message = `An error occured ${err.error.message}`;
          } else {
            this.message = `Backend returned error code ${err.status}, body was: ${err.message}`;
          }
        }
      );
  }
}

原文地址:https://www.cnblogs.com/Answer1215/p/8448860.html

时间: 2024-11-08 21:40:08

[Angular] Handle HTTP Errors in Angular with HttpErrorResponse interface的相关文章

angular的跨域(angular百度下拉提示模拟)和angular选项卡

1.angular中$http的服务: $http.get(url,{params:{参数}}).success().error(); $http.post(url,{params:{参数}}).success().error(); $http.jsonp(url,{params:{wd:'',cb:'JSON_CALLBACK'}}).success().error(); 注意jsonp中cb在angular中规定只能使用JSON_CALLBAC $watch(谁,做什么,false): 谁指

Access-Control-Allow-Origin: Dealing with CORS Errors in Angular

https://daveceddia.com/access-control-allow-origin-cors-errors-in-angular/ Getting this error in your Angular app? No ‘Access-Control-Allow-Origin’ header is present on the requested resource. You’ve run afoul of the Same Origin Policy – it says that

【angular.js】UI-Router之angular路由学习

AngularJs中的路由,应用比较广泛,主要是允许我们通过不同的url访问不同的内容,可实现多视图的单页web应用.下面看看具体怎么使用. 关于路由 通常我们的URL形式为http://jtjds.cn/first/page,但在单页web应用中angularjs通过#+标记实现,比如下面的页面,将是下文中的路由列子. http://192.168.1.109:8000/angular-program/src/main.html#/pagetable/page1 http://192.168.

简话Angular 01 为什么要学Angular

1. JQuery vs Javascipt 问两个问题: 1) 你用过JQuery吗?当然! 2) 感觉JQuery相对于原生纯Javascript优势在哪里? 我的答案:JQuery更直接方便,兼容性好,代码少,好理解,快速开发,Bug少…… 2. Angular vs JQuery 参照JQuery vs Javascript 3. Angular的前途怎么样 假如今天是2008年,有人问JQuery前途怎么样,你知道吗?现在你知道JQuery前途了吗? 4. Angular可以不学吗 不

[Angular 2] BYPASSING PROVIDERS IN ANGULAR 2

Artical --> BYPASSING PROVIDERS IN ANGULAR 2 Here trying to solve one problem: On the left hand side of tree, there are 4 green blocks and 1 blue block. Meaning that three green dataService will use 'OtherProvider' which in an instance of DataService

[Angular 2] Generate and Render Angular 2 Template Elements in a Component

Angular 2 Components have templates, but you can also create templates inside of your templates using Angular 2 ViewContainer’s createEmbeddedView for more visual control of how you want to generate elements inside of your Angular 2 templates. import

在Angular外部使用js调用Angular控制器中提供的函数方法或变量

Html代码如下所示: 1 <!DOCTYPE html> 2 <html ng-app="myApp" id="myApp"> 3 <head> 4 <meta name="viewport" content="width=device-width" /> 5 <title>Test</title> 6 <script src="~/Co

[Angular Directive] 1. Write an Angular Directive

Angular 2 Directives allow you manipulate elements by adding custom behaviors through attributes. This lesson covers how to create a Directive and attach its behavior to an element. import {Directive, HostBinding} from '@angular/core'; @Directive({ s

[Angular 2] Move and Delete Angular 2 Components After Creation

After the original order is set, you can still leverage methods on the Angular 2 ViewContainer to reorder the components. This is especially helpful when you want an event to trigger layout changes from your generated components. import { Component,