当前位置: 首页 > 编程笔记 >

Angular 2 简单类扩展angular的Http类

嵇弘新
2023-03-14
本文向大家介绍Angular 2 简单类扩展angular的Http类,包括了Angular 2 简单类扩展angular的Http类的使用技巧和注意事项,需要的朋友参考一下

示例

import { Http, Request, RequestOptionsArgs, Response, RequestOptions, ConnectionBackend, Headers } from '@angular/http';
import { Router } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/empty';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import { ApplicationConfiguration } from '../application-configuration/application-configuration';

/**
* This class extends the Http class from angular and adds automaticaly the server URL(if in development mode) and 2 headers by default:
* Headers added: 'Content-Type' and 'X-AUTH-TOKEN'.
* 'Content-Type' can be set in any othe service, and if set, it will NOT be overwritten in this class any more.
*/
export class HttpServiceLayer extends Http {

  constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private _router: Router, private appConfig: ApplicationConfiguration) {
    super(backend, defaultOptions);
  }

  request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
    this.getRequestOptionArgs(options);
    return this.intercept(super.request(this.appConfig.getServerAdress() + url, options));
  }

  /**
  * This method checks if there are any headers added and if not created the headers map and ads 'Content-Type' and 'X-AUTH-TOKEN'
  * 'Content-Type' is not overwritten if it is allready available in the headers map
  */
  getRequestOptionArgs(options?: RequestOptionsArgs): RequestOptionsArgs {
    if (options == null) {
      options = new RequestOptions();
    }
    if (options.headers == null) {
     options.headers= new Headers();
    }

    if (!options.headers.get('Content-Type')) {
      options.headers.append('Content-Type', 'application/json');
    }

    if (this.appConfig.getAuthToken() != null) {
      options.headers.append('X-AUTH-TOKEN', this.appConfig.getAuthToken());
    }

    return options;
  }

  /**
  * This method as the name sugests intercepts the request and checks if there are any errors.
  * If an error is present it will be checked what error there is and if it is a general one then it will be handled here, otherwise, will be
  * thrown up in the service layers
  */
  intercept(observable: Observable<Response>): Observable<Response> {

    //  返回可观察到的;
    return observable.catch((err, source) => {
      if (err.status == 401) {
        this._router.navigate(['/login']);
        //返回可观察到的;
        return Observable.empty();
      } else {
        //返回可观察到的;
        return Observable.throw(err);
      }
    });
  }
}
           

 类似资料:
  • 问题内容: 我有一个基类,我想在服务中进行扩展以帮助将数据输入角度范围。我已经在网上搜索了一种解决方案,但是没有找到我喜欢的解决方案。我有一个用于访问设备文件系统的基类 类结构: 我希望能够在几个不同的角度服务(即offlineCart,offlineCustomLists等)中扩展该类,其中每个服务都将能够使用存储库来存储各种不同的数据类型。我正在寻找最好,最合适的方式来做到这一点。在普通Jav

  • 问题 你想不依靠其他工具,直接使用Python的扩展API来编写一些简单的C扩展模块。 解决方案 对于简单的C代码,构建一个自定义扩展模块是很容易的。 作为第一步,你需要确保你的C代码有一个正确的头文件。例如: /* sample.h */ #include <math.h> extern int gcd(int, int); extern int in_mandel(double x0, d

  • 我在Android应用程序中有一个配置类,存储在Realm数据库中。我只是按照单例设计模式对其进行了编辑,所以配置只有一个实例。 问题是,该类现在有一个私有构造函数,可以在不调用我的“getInstance”方法的情况下防止实例化。然而,RealmObject似乎需要一个公共构造函数。 难道不可能有一个类使用单例设计模式扩展RealmObject吗? 这仅仅是我必须解释的领域的限制吗? 下面是上下

  • 本文向大家介绍简单介绍C# 中的扩展方法,包括了简单介绍C# 中的扩展方法的使用技巧和注意事项,需要的朋友参考一下     扩展方法是C#3.0引入的新特性,使用它,可以在不修改某一类的代码的情况下,实现该类方法的扩展。     为一个类添加扩展方法,需要三个要素:         1.扩展方法所在的类为静态类         2.扩展方法本身要为静态方法         3.扩展方法的第一个参数

  • This section is intended as a walkthrough for the creation of custom extensions. It covers the basics of writing and activating an extensions, as well as commonly used features of extensions. As an ex

  • 验证码生成 缓存支持