【问题标题】:Angular injecting toastr serviceAngular 注入 toastr 服务
【发布时间】:2017-07-25 06:49:10
【问题描述】:

我想使用角度材料 mdsnackbar 服务来处理常见的 http 错误,但是,我不知道如何实现它。如果我将 MdSnackBar 添加到 private 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;
  }
}

toastrservice

 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.tsapp.module.ts 都有更新依赖项,它的工作原理就像一个魅力。

【问题讨论】:

    标签: angular angular-services toastr angular-toastr


    【解决方案1】:

    你需要将它添加到构造函数参数列表中

    @Injectable()
    
    export class HttpInterceptorService extends Http {
      constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private toastr:ToastrService) {
        super(backend, defaultOptions);
      }
    

    您还需要在某处提供ToastrService,以便注入。

    @NgModule({
      ...,
      providers: [ToastrService], 
    }}
    

    MdSnackBar 也需要在某处提供,例如通过导入提供它的模块。

    【讨论】:

    • 我已经编辑了我的问题。谢谢你的启发:)
    猜你喜欢
    • 1970-01-01
    • 2014-09-19
    • 2017-08-24
    • 1970-01-01
    • 2016-12-09
    • 2023-03-25
    • 1970-01-01
    • 2019-03-04
    • 1970-01-01
    相关资源
    最近更新 更多