Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Http “逃避”;“使用工厂”;在Angular2中过度引用提供程序时_Http_Angular_Factory_Provider - Fatal编程技术网

Http “逃避”;“使用工厂”;在Angular2中过度引用提供程序时

Http “逃避”;“使用工厂”;在Angular2中过度引用提供程序时,http,angular,factory,provider,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.subscr

我的问题与@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,我需要使用该拦截器还是不使用该拦截器

我在找什么

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

拦截器内部


最好知道问题的确切原因是什么?:-)是的,很抱歉。问题是,我正在发送一个http请求,以获取代码中的json文件。并且在通过useFactory代码块时找不到如何实现该操作。我想继续现在的操作,有点“下一步”的观察。thnx@user1713747请给出一个问题来解释这个问题。我使用了这个Http拦截器示例。也许有帮助。
if(url === 'useinterceptor) {
  // USE INTERCEPTOR
}
if(url === 'notuseinterceptor') {
  // USE HTTP GET OPERATION AS IT IS 
}