Javascript angularjs $httpProvider 拦截器文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26171986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:27:11  来源:igfitidea点击:

angularjs $httpProvider interceptor documentation

javascriptangularjshttpinterceptorangular-services

提问by webberpuma

I am new to angular (and programming), here is a seemingly simple question but I could not figure it out.

我是角度(和编程)的新手,这是一个看似简单的问题,但我无法弄清楚。

some tutorials suggests using $httpProvider.interceptors.push('interceptorName')to manipulate the http request and response.

一些教程建议使用$httpProvider.interceptors.push('interceptorName')来操作 http 请求和响应。

I want to know more about the interceptor thing so I look at the official document, but I could not find anything related to interceptor, there are only a method (useApplyAsync([value]);) and a property (defaults) in $httpProvider(docs).

我想更多地了解拦截器的事情,所以我查看了官方文档,但是我找不到与拦截器相关的任何内容,只有一个方法 (useApplyAsync([value]);) 和一个属性 (defaults) in $httpProvider( docs)。

I know from other tutorials that an interceptor is a regular service factory and I know how to use it, but my question is: since the syntax is $httpProvider.interceptors.push('interceptorName'), then I expect I will find a property called "interceptors" in $httpProvider, but in fact I can't. Is something I miss to get this confusion? or is my concept totally wrong from the bottom?

我从其他教程中知道拦截器是一个常规的服务工厂,我知道如何使用它,但我的问题是:既然语法是$httpProvider.interceptors.push('interceptorName'),那么我希望我会在 中找到一个名为“拦截器”的属性$httpProvider,但实际上我可以'不。我想念这种混乱吗?还是我的概念从根本上是完全错误的?

回答by m59

Interceptors are in the documentation here.

拦截器位于此处文档中

Here's an example of how to write one.

这是一个如何写一个的例子。

.config([
  '$httpProvider',
  function($httpProvider) {

    var interceptor = [
      '$q',
      '$rootScope',
      'userSession',
      function($q, $rootScope, userSession) {

        var service = {

          // run this function before making requests
          'request': function(config) {

            if (config.method === 'GET' || userSession.isAuth()) {
              // the request looks good, so return the config
              return config;
            }

            // bad request, so reject
            return $q.reject(config);

          }

        };

        return service;

      }
    ];

    $httpProvider.interceptors.push(interceptor);

  }
])

The reason there's nothing on the $httpProviderdocumentation page about interceptors is because the developers didn't include the following code in the $httpscript which the docs are generated from:

$httpProvider文档页面上没有关于拦截器的原因是因为开发人员没有在$http生成文档脚本中包含以下代码:

/**
   * @ngdoc property
   * @name $httpProvider#interceptors
   * @description
// etc

Documentation in general is known to be incomplete, inaccurate, and/or confusing. Until recently, I always thought I was the problem when I couldn't find or understand something, but I've found that it's often because documentation is just lousy. However, we should all be thankful that we have such great tools to use and keep in mind that perhaps the documentation is poor because time had to be focused on writing the tool instead of the manual for the tool.

众所周知,文档通常不完整、不准确和/或令人困惑。直到最近,当我找不到或无法理解某些东西时,我一直认为是我的问题,但我发现这通常是因为文档太糟糕了。然而,我们都应该感谢我们有如此出色的工具可供使用,并记住,也许文档很差,因为时间必须集中在编写工具而不是工具手册上。

The most reliable "documentation" is the source code itself, though it can be a lot less friendly to read! In the source code I linked above, you can see this.interceptors = []. thisrefers to the $httpProvider, so it is assigning the property interceptorsto $httpProviderwith the value being an empty array. To add your interceptors, you simply push()your interceptor to this array.

最可靠的“文档”是源代码本身,尽管阅读起来可能不太友好!在我上面链接的源代码中,您可以看到this.interceptors = []. this指的是$httpProvider,因此被分配属性interceptors,以$httpProvider与所述值是一个空数组。要添加拦截器,只需push()将拦截器添加到此数组即可。