在Angular2中重写提供程序时逃避“useFactory”

时间:2017-01-09 16:30:47

标签: http angular factory provider

我的问题与@ angular / http的Http模块的覆盖有关。我正在使用下一个代码拦截每个Http请求:

export let fakeBackendProvider = {
  // intercepts every single Http call
  provide: Http,
  useFactory: (backend: MockBackend, options: BaseRequestOptions) => {
    backend.connections.subscribe((connection) => {
       // here I treat requests for their urls
       if(connection.endsWith('/example/') {
           // ...
       }

       // here I'd like to CONTINUE the normal execution as it is,
       // as if THIS call had never entered in "useFactory" peace of code
       if(connection.endsWith('/example.noprovider/') {
           // GO ON
       }
    })
    return new Http(backend, options)
  },
  deps: [MockBackend, BaseRequestOptions]
}

我需要什么

  • 通过HTTP请求从JSON文件获取数据

问题

我的拦截器(上面的代码)总是站在HTTP调用之前,但是根据HTTP请求的URL,我需要使用那个拦截器。

我在寻找什么

“分离”例程的一些功能,如:

INSIDE THE INTERCEPTOR

if(url === 'useinterceptor) {
  // USE INTERCEPTOR
}
if(url === 'notuseinterceptor') {
  // USE HTTP GET OPERATION AS IT IS 
}

0 个答案:

没有答案