我想使用角度材料 mdsnackbar 服务处理常见的 http 错误,但是,我无法弄清楚如何实现它。private mdsnackbar: MdSnackBar
如果我将 MdSnackBar 添加到构造函数,例如导致类本身使用,它会给我一个错误,例如不匹配任何参数类型super
我想知道是否有另一种方法可以达到相同的结果。
http拦截器
import { Injectable } from '@angular/core';
import {
ConnectionBackend,
RequestOptions,
Request,
RequestOptionsArgs,
Response,
Http,
Headers,
} from '@angular/http';
import { ToastrService } from './toastr.service'
import { MdSnackBar } from '@angular/material';
import { Observable } from 'rxjs/Rx';
import { environment } from '../environments/environment';
@Injectable()
export class HttpInterceptorService extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions) {
super(backend, defaultOptions);
}
request(
url: string | Request,
options?: RequestOptionsArgs
): Observable<Response> {
return super.request(url, options).catch(this.catchErrors());
}
catchErrors() {
return (res: Response) => {
console.log(res);
if (res.status === 401) {
// this.toastrService.showToaster('Hello World');
return Observable.throw(res);
}
return Observable.throw(res);
};
}
get(url: string, options?: RequestOptionsArgs): Observable<Response> {
url = this.updateUrl(url);
console.log(url);
return super.get(url, this.getRequestOptionArgs(options));
}
post(
url: string,
body: string,
options?: RequestOptionsArgs
): Observable<Response> {
url = this.updateUrl(url);
return super.post(url, body, this.getRequestOptionArgs(options));
}
put(
url: string,
body: string,
options?: RequestOptionsArgs
): Observable<Response> {
url = this.updateUrl(url);
return super.put(url, body, this.getRequestOptionArgs(options));
}
delete(url: string, options?: RequestOptionsArgs): Observable<Response> {
url = this.updateUrl(url);
return super.delete(url, this.getRequestOptionArgs(options));
}
private updateUrl(req: string) {
return environment.origin + req;
}
private getRequestOptionArgs(
options?: RequestOptionsArgs
): RequestOptionsArgs {
if (options == null) {
options = new RequestOptions();
}
if (options.headers == null) {
options.headers = new Headers();
}
options.headers.append('Content-Type', 'application/json');
return options;
}
}
吐司服务
import { Injectable } from '@angular/core';
import { MdSnackBar } from '@angular/material';
@Injectable()
export class ToastrService {
constructor(private snackBar: MdSnackBar) {
}
showToaster(msg: string) {
this.snackBar.open(msg, null, {
duration: 3000,
});
}
}
http.service.ts
import { XHRBackend, Http, RequestOptions } from '@angular/http';
import { HttpInterceptorService } from './shared/http-interceptor.service';
import { ToastrService } from './shared/toastr.service'
export function httpService(
xhrBackend: XHRBackend,
requestOptions: RequestOptions,
toastr: ToastrService // forget to insert it
): Http {
return new HttpInterceptorService(xhrBackend, requestOptions, toastr);
}
app.module.ts
providers: [
DataserviceService,
HttpInterceptorService,
ToastrService,
{
provide: Http,
useFactory: httpService,
deps: [XHRBackend, RequestOptions, ToastrService],
},
我对两者都有更新依赖项http.service.ts
,app.module.ts
它就像一个魅力。