【发布时间】:2016-06-24 14:55:27
【问题描述】:
我正在尝试向我的所有 Web 请求添加一个参数,以便强制禁用缓存。我要做的就是添加
?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 无济于事。
我确定我遗漏了一些微小的东西,或者我已经把它复杂化了(向所有 get 请求添加单个参数有多难?)。
【问题讨论】:
-
@AngelAngel 谢谢,但我以前见过,plunk 在打字稿中,文档虽然很好,但没有解释如何在 ES5 中做到这一点。
标签: javascript angularjs angular