无法添加" $ localStorage"

时间:2017-10-24 15:14:49

标签: javascript angularjs node.js

我在app.js中开发了methode,但是我有这个错误:Uncaught Error:
 [$ injector:strictdi]函数($ rootScope,$ q,$ localStorage,$ location)未使用显式注释,无法在严格模式下调用 http://errors.angularjs.org/1.6.6/ $ injector / strictdi?p0 =功能(%24rootScope%2C%20%24q%2C%20%24localStorage%2C%20%24location)

angular.module('ppollitApp', [ngCookies, ngResource, ngSanitize, 'btford.socket-io', uiRouter,
      uiBootstrap, _Auth, account, admin, navbar, footer, main, constants, socket, util, profile, explore
    ])
      .config(routeConfig)
      .factory('authInterceptor', function($rootScope, $q, $localStorage, $location) {
        return {
          // Add authorization token to headers
          request: function(config) {
            config.headers = config.headers || {};
            if($localStorage.token) {
              config.headers.Authorization = 'Bearer ' + $localStorage.token;
            }
            return config;
          },

          // Intercept 401s and redirect you to login
          responseError: function(response) {
            if(response.status === 401) {
              $location.path('/login');
              // remove any stale tokens
              delete $localStorage.token;
              return $q.reject(response);
            } else {
              return $q.reject(response);
            }
          }
        };
      })

      .run(function($rootScope, $location, Auth) {
        'ngInject';
        // Redirect to login if route requires auth and you're not logged in

        $rootScope.$on('$stateChangeStart', function(event, next) {
          Auth.isLoggedIn(function(loggedIn) {
            if(next.authenticate && !loggedIn) {
              $location.path('/login');
            }
          });
        });
      });

    angular.element(document)
      .ready(() => {
        angular.bootstrap(document, ['ppollitApp'], {
          strictDi: true
        });
      });

    var module = angular.module('main', ['ppollitApp', 'ppollitApp.services']);

    module.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
      $routeProvider.when('/', { templateUrl: '/login/login.html', controller: 'LoginController'});

      $locationProvider.html5Mode(true);
    }]);

2 个答案:

答案 0 :(得分:1)

错误意味着它所说的内容,使用DI的所述函数不使用显式注释,并且不能在严格模式下调用。

The page that the error links to包含所有必要信息。

考虑到ng-annotate已经用于DI注释,它应该是:

  ...
  .factory('authInterceptor', function($rootScope, $q, $localStorage, $location) {
    'ngInject';
    ...

答案 1 :(得分:0)

从以下地址更改工厂:

angular.factory('authInterceptor', function($rootScope, $q, $localStorage, $location) { /* ... */ }

要:

authInterceptor.$inject = ['$rootScope', '$q', '$localStorage', '$location'];
angular.factory('authInterceptor', authInterceptor);
function authInterceptor($rootScope, $q, $localStorage, $location) { /* ... */ }