Angular 角注入toastr服务
我想使用angular materials mdsnackbar服务处理常见的http错误,但是,我不知道如何实现它。它给了我一个错误,例如如果我将MdSnackBar添加到构造函数中,则不匹配任何参数类型,如Angular 角注入toastr服务,angular,angular-services,toastr,angular-toastr,Angular,Angular Services,Toastr,Angular Toastr,我想使用angular materials mdsnackbar服务处理常见的http错误,但是,我不知道如何实现它。它给了我一个错误,例如如果我将MdSnackBar添加到构造函数中,则不匹配任何参数类型,如私有MdSnackBar:MdSnackBar导致类本身使用super 我想知道是否有其他方法可以达到同样的效果 http拦截器 import { Injectable } from '@angular/core'; import { ConnectionBackend, Requ
私有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;
}
}
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);
}
providers: [
DataserviceService,
HttpInterceptorService,
ToastrService,
{
provide: Http,
useFactory: httpService,
deps: [XHRBackend, RequestOptions, ToastrService],
},
应用程序模块.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);
}
providers: [
DataserviceService,
HttpInterceptorService,
ToastrService,
{
provide: Http,
useFactory: httpService,
deps: [XHRBackend, RequestOptions, ToastrService],
},
我对
http.service.ts
和app.module.ts
都有更新依赖项,它工作起来就像一个charmm。您需要将它添加到构造函数参数列表中
@Injectable()
export class HttpInterceptorService extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private toastr:ToastrService) {
super(backend, defaultOptions);
}
您还需要在某个地方提供ToastrService
,以便将其注入
@NgModule({
...,
providers: [ToastrService],
}}
MdSnackBar
也需要在某个地方提供,例如通过导入提供它的模块。您需要将它添加到构造函数参数列表中
@Injectable()
export class HttpInterceptorService extends Http {
constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private toastr:ToastrService) {
super(backend, defaultOptions);
}
您还需要在某个地方提供ToastrService
,以便将其注入
@NgModule({
...,
providers: [ToastrService],
}}
MdSnackBar
也需要在某个地方提供,例如导入提供它的模块。我已经编辑了我的问题。谢谢你的启发:)我已经编辑了我的问题。谢谢你的启发:)