【问题标题】:Uncaught Error: [$injector:unpr] Unknown provider: $cookiesProvider <- $cookies <- YourHttpInterceptor <- $http <- ng1UIRouter未捕获的错误:[$injector:unpr] 未知提供程序:$cookiesProvider <- $cookies <- YourHttpInterceptor <- $http <- ng1UIRouter
【发布时间】:2016-10-02 18:03:30
【问题描述】:

我遇到了这个错误,花了好几个小时才解决这个问题。我是 MEAN 堆栈开发的新手。 我正在尝试在我的应用程序中实现会话管理。但卡在了这一点上。

错误:

未捕获的错误:[$injector:unpr] 未知提供程序:$cookiesProvider

angular.module('MyApp')
.factory('YourHttpInterceptor', ['$q', '$cookies', '$location',
function($q, $cookies, $location) {


   return {        
        'request': function(config) {

            console.log("req");
            config.headers = config.headers || {};
              if ($cookies.get('token')) {
                config.headers.Authorization = 'Bearer ' + $cookies.get('token');
              }
            return config;
        },       

        // Optional method
        'requestError': function(rejection) {
            // do something on request error 
            console.log("inside the request error");
            if (canRecover(rejection)) {
                return responseOrNewPromise
            }
            return $q.reject(rejection);
        },        

        // Optional method        
        'response': function(response) {
            // do something on response success
            console.log("inside the response ");
            return response;
        },

        // optional method 
        responseError: function(response) {
          if(response.status === 401) {
            $location.path('/login');
            // remove any stale tokens
            $cookies.remove('token');
            return $q.reject(response);
          }
          else {
            return $q.reject(response);
          }
        }
    };
}]);

谢谢。


添加“ngCookies”后 错误:

未捕获的错误:[$injector:modulerr] 无法实例化模块 MyApp 由于:错误:[$injector:modulerr] 无法实例化 模块 ngCookies 由于:错误:[$injector:nomod] 模块 'ngCookies' 不可用!您要么拼错了模块名称,要么忘记了 加载它。如果注册一个模块,请确保您指定 依赖项作为第二个参数。

app.js

angular.module('MyApp', [
    'ngMaterial',
    'ngMdIcons',
    'ui.router',
    'ngCookies',
    'e3-core-ui.services', 
    'e3-core-ui.utils'  
])

.config(['$stateProvider', '$routeProvider','$httpProvider','$mdThemingProvider', '$mdIconProvider',  function($stateProvider, $routeProvider, $httpProvider, $mdThemingProvider, $mdIconProvider) { 
 $httpProvider.interceptors.push('YourHttpInterceptor');
...
enter code here

【问题讨论】:

  • 您是否将ngCookies 添加为应用程序的依赖项?
  • 可以添加用于创建应用程序的代码吗?
  • 基本上,我想实现会话管理。因此,如果您有任何可以帮助我的代码...我可以更新此代码。 (当我在我的应用程序中添加“ngCookies”时,它会产生一堆错误。)
  • @gnerkus 如果您不介意并且有时间,我可以分享我的 teamviewer 详细信息。
  • 如果您能展示应用的创建方式以及添加ngCookies时显示的错误,会更容易为您提供帮助。

标签: angularjs node.js cookies angular-http-interceptors


【解决方案1】:

在您的 index.html 页面中,您必须在加载 angular.js 后加载 angular-cookies:

示例

<!-- Not minified, version 1.5.5 -->
<script src="https://code.angularjs.org/1.5.5/angular-cookies.js"></script>
<!-- Minified, version 1.5.5 -->
<script src="https://code.angularjs.org/1.5.5/angular-cookies.min.js"></script>

注意:尝试将您的版本设置为相同,如果您使用的是 Angular 1.5.5,那么这很好。如果没有,请确保根据需要设置您的版本。

【讨论】:

  • ngCookies 需要包含 angular.module('MyApp', [ 'ngCookies', ])
【解决方案2】:

第 1 步: 首先包含最新的库:

第 2 步:包含

angular.module('MyApp', [ 'ngCookies', ])

第 3 步:然后在任何自定义服务中调用 $cookies。

喜欢: app.factory('myCookies', ['$cookies', function($cookies) { var profileData = {};

profileData.setCookieData = function (userName, userId) 
{
    $cookies.put("userName", userName);
    $cookies.put("userId", userId);
};

profileData.getUserName = function () 
{
    return $cookies.get("userName");
};

profileData.getUserId = function () 
{
    return $cookies.get("userId");
};

profileData.remove = function () 
{
    $cookies.remove("userName");
    $cookies.remove("userId");
};

return profileData;

}]);

仅此而已。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多