【发布时间】:2017-10-20 05:36:52
【问题描述】:
我想在 Angular 中编写可以拦截所有 ajax 调用的可注入服务。基本上在 ajaxStart 之前和完成之后。我可以通过这个code-sn-ps来实现。但我可以使用 es5 语法来实现它。我怎样才能通过扩展文件号:3 中显示的XMLHttpRequest 来做同样的事情?
1) http-interceptor.ts
import { Injectable, Component, OnInit } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
interface AjaxRequest {
url?: string;
requestCount?: number;
method?: string;
}
interface AjaxResponse {
url?: string;
requestCount?: number;
response?: string;
}
@Injectable()
export class HttpInterceptor {
public ajaxStart = new BehaviorSubject<AjaxRequest>({});
public ajaxStop = new BehaviorSubject<AjaxResponse>({});
constructor() {
this.bootstrapAjaxInterceptor();
}
private bootstrapAjaxInterceptor() {
const _self = this;
const originalOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function (xhrMethod, requestUrl) {
_self.ajaxStart.next(requestUrl);
this.addEventListener('readystatechange', xhr => {
_self.ajaxStop.next(this.responseURL);
}, false);
originalOpen.apply(this, arguments);
};
}
}
2) 应用组件.ts
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
constructor(private httpInterceptor: HttpInterceptor) { }
ngOnInit() {
this.httpInterceptor.ajaxStart.subscribe(url => {
console.log('ajaxStart : ', url);
});
this.httpInterceptor.ajaxStop.subscribe(url => {
console.log('ajaxStop : ', url);
});
}
}
3) http-interceptor.ts
@Injectable()
export class HttpInterceptor extends XMLHttpRequest {
open(xhrMethod, requestUrl) {
// Equivalent to XMLHttpRequest.prototype.open
// Now how to handle `readystatechange`
}
ajaxStart() { }
ajaxStop() { }
}
【问题讨论】:
-
感谢@Bahrampour 的更正。如果你觉得这是个好问题。你可以投票吗?
标签: javascript angularjs dependency-injection ecmascript-6