简体   繁体   English

Angular 2中的HTTP转换请求

[英]HTTP Transform request in Angular 2

I am attempting to add a single parameter to all my web requests so that caching is forcibly disabled. 我试图将一个参数添加到我的所有Web请求中,以便强制禁用缓存。 All I want to do is add 我要做的就是添加

?v=1535DC9D930 // Current timestamp in hex

to the end of every request. 到每个请求的结尾。

I am writing this in plain ES5 JS, but all the documentation is in Typescript which is taking a little while to convert. 我是用普通的ES5 JS编写的,但是所有文档都在Typescript中,这需要花费一些时间进行转换。 I have the following so far: 到目前为止,我有以下内容:

(function(app) {
    app.CustomHttp = ng.core
        .Class({
            constructor: [ng.http.Http, function(http) {
                console.log(this);
                this.http = http;
            }],
            request: function() {
                return this.http.request.apply(arguments);
            },
            get: function() {
                return this.http.get.apply(arguments);
            },
            post: function() {
                return this.http.post.apply(arguments);
            },
            put: function() {
                return this.http.put.apply(arguments);
            },
            delete: function() {
                return this.http.delete.apply(arguments);
            },
            patch: function() {
                return this.http.patch.apply(arguments);
            },
            head: function() {
                return this.http.head.apply(arguments);
            }
        });
})(window.app || (window.app = {}));

(function(app) {
    document.addEventListener('DOMContentLoaded', function() {
        ng.core.enableProdMode();
        ng.platform.browser.bootstrap(
            app.AppComponent,
            [ng.core.provide(ng.http.Http, { useClass: app.CustomHttp })]
        ).catch(function(error) {
            console.error(error);
        });
    });
})(window.app || (window.app = {}));

The app fully works, yet the console.log(this) in the CustomHttp never gets called, and when I inspect ng.http.HTTP_PROVIDERS in the browser it doesn't appear to be using CustomHttp . 该应用程序完全可以运行,但是CustomHttpconsole.log(this)从未被调用,当我在浏览器中检查ng.http.HTTP_PROVIDERS时,它似乎没有使用CustomHttp I have also tried: 我也尝试过:

[ng.http.HTTP_PROVIDERS, ng.core.provide(ng.http.Http, { useClass: app.CustomHttp })]

In my boot.js to no avail. 在我的boot.js中无济于事。

I'm sure there is something tiny that I am missing, or that I have massively over complicated this (How hard can it be to add a single parameter to all get requests?). 我确定我缺少一些微小的东西,或者我对此过于复杂了(将单个参数添加到所有get请求到底有多难?)。

Intercepting HTTP requests with ES5 only isn't so easy mainly because you don't have the support of super . 仅通过ES5拦截HTTP请求并不是那么容易,主要是因为您没有super的支持。

First create a function to save the request , get , ... methods of the ng.http.Http object into equivalent _request , _get ones. 首先创建一个函数,将ng.http.Http对象的requestget和...方法保存到等效的_request_get方法中。 This allows to simulate something like super.get() . 这允许模拟类似super.get()东西。 Otherwise they will be overriden (and lost) when defining your own methods. 否则,在定义您自己的方法时,它们将被覆盖(并丢失)。

function createHttpClass() {
  var Http = function() {}
  Http.prototype = Object.create(ng.http.Http.prototype);
  Http.prototype._request = Http.prototype.request;
  Http.prototype._get = Http.prototype.get;
  Http.prototype._post = Http.prototype.post;
  Http.prototype._put = Http.prototype.put;
  Http.prototype._delete = Http.prototype.delete;
  Http.prototype._head = Http.prototype.head;
  return Http;
}

Then you can create a custom HttpClass that extends the ng.core.Http one: 然后,您可以创建一个扩展ng.core.Http的自定义HttpClass

var CustomHttp = ng.core
  .Class({
    constructor: function(_backend, _defaultOptions) {
      this._backend = _backend;
      this._defaultOptions = _defaultOptions;
    },
    extends: createHttpClass(),
    request: function() {
      console.log('request');
      return this._request.apply(this, arguments);
    },
    get: function() {
      console.log('get');
      return this._get.apply(this, arguments);
    },
    (...)
  });

You can notice that I leverage the extends attribute with the object create using the createHttpClass function. 您可能会注意到,我通过使用createHttpClass函数创建的对象利用了extends属性。

You need finally to register the provider for this CustomHttp class: 您最终需要为该CustomHttp类注册提供程序:

ng.platform.browser.bootstrap(AppComponent, [
  ng.http.HTTP_PROVIDERS,
  ng.core.provide(ng.http.Http, {
    useFactory: function(backend, defaultOptions) {
      return new CustomHttp(backend, defaultOptions);
    },
    deps: [ng.http.XHRBackend, ng.http.RequestOptions]
  })
]);

Here is the corresponding plunkr: https://plnkr.co/edit/tITkBJcl4a5KVJsB7nAt . 这是相应的plunkr: https ://plnkr.co/edit/tITkBJcl4a5KVJsB7nAt。

This implementation is inspired from the TypeScript one. 此实现是从TypeScript中获得灵感的。 See this question for more details: 有关更多详细信息,请参见此问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM