我正在嘗試向我的所有Web請求中添加一個參數,以便緩存被強制禁用。我想要做的只是添加Angular 2中的HTTP轉換請求
?v=1535DC9D930 // Current timestamp in hex
到每個請求的結尾。
我在純ES5 JS中寫這篇文章,但所有的文檔都是在需要一點時間轉換的Typescript中。我見到目前爲止如下:
(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 = {}));
的應用程序完全適用,但在CustomHttp
不會被調用的console.log(this)
,當我在瀏覽器中檢查ng.http.HTTP_PROVIDERS
它似乎沒有使用CustomHttp
。我也試過:
[ng.http.HTTP_PROVIDERS, ng.core.provide(ng.http.Http, { useClass: app.CustomHttp })]
在我的boot.js
無濟於事。
我敢肯定,有一些我很想念的東西,或者我已經大量複雜了(這對於向所有獲取請求添加單個參數有多難?)。
http://plnkr.co/edit/6w8XA8YTkDRcPYpdB9dk?p=preview我不太瞭解,但也許這可以幫助你https://開頭的角度。 io/docs/js/latest/api/http/RequestOptions-class.html https://angular.io/docs/js/latest/api/http/RequestMethod-enum.html –
@AngelAngel謝謝,但我見過那些之前,plunk是在打字稿和文檔中,雖然不錯,但並沒有解釋如何在ES5中做到這一點。 – JosephGarrone