使用John Papa的样式指南语法编写的提供程序的缩小问题

时间:2015-07-19 16:58:20

标签: angularjs

我使用John Papa的风格指南写了一个提供者,即使他没有提供有关提供者的任何规则。当我在我的应用程序的运行块中注入我的服务时,我收到以下错误:

Uncaught Error: [$injector:strictdi] auth is not using explicit annotation and cannot be invoked in strict mode

运行块:

angular.module('app', ['rest-client'])
.run(runBlock);

runBlock.$inject = ['Restangular', '$http', 'auth'];

/* @ngInject */
function runBlock(Restangular, $http, auth) {
}

我的提供者:

(function () {
    'use strict';

    angular
        .module('rest-client')
        .provider('auth', authProvider);

    authProvider.$inject = [];

    /* @ngInject */
    function authProvider() {

        var config = {
            authBaseUrl: undefined
        };

        var serviceProvider = {
            setAuthBaseUrl: setAuthBaseUrl,
            $get: $get
        };

        return serviceProvider;

        function setAuthBaseUrl(authBaseUrl) {
            config.authBaseUrl = authBaseUrl;
        };

        $get.$inject = ['localStorageService', 'Restangular', '$q', '$http'];

        /* @ngInject */
        function $get(localStorageService, Restangular, $q, $http) {
            return authService(localStorageService, Restangular, $q, $http);
        };

        authService.$inject = ['localStorageService', 'Restangular', '$q', '$http'];
        /* @ngInject */
        function authService(localStorageService, Restangular, $q, $http) {

            var service = {
                getAccessToken: getAccessToken,
                setAccessToken: setAccessToken,
                getRefreshToken: getRefreshToken,
                setRefreshToken: setRefreshToken,
                deleteTokens: deleteTokens,
                logout: logout,
                refreshToken: refreshToken
            };

            return service;

            ////////////////

            function getAccessToken() {
                var token = localStorageService.get('access-token');
                return token;
            }

            function setAccessToken(token) {
                localStorageService.set('access-token', token);
                Restangular.setDefaultHeaders({Authorization: 'Bearer ' + this.getAccessToken()});
            }

            function getRefreshToken() {
                var token = localStorageService.get('refresh-token');
                return token;
            }

            function setRefreshToken(token) {
                localStorageService.set('refresh-token', token);
            }

            function deleteTokens() {
                localStorageService.remove('access-token');
                localStorageService.remove('refresh-token');
            }

            function logout() {
                localStorageService.clearAll();
                Restangular.setDefaultHeaders({Authorization: 'Bearer ' + this.getAccessToken()});
            }

            function refreshToken() {
                var deferred = $q.defer();
                $http.get(config.authBaseUrl + '/auth/refresh/' + this.getRefreshToken()).then(function (result) {
                    var accessToken = result.data.accessToken;
                    this.setAccessToken(accessToken);
                    deferred.resolve(accessToken);
                });
                return deferred.promise;
            }

        }

    }
})();

1 个答案:

答案 0 :(得分:0)

    return serviceProvider;

之前运行
    $get.$inject = ['localStorageService', 'Restangular', '$q', '$http'];

    authService.$inject = ['localStorageService', 'Restangular', '$q', '$http'];

后者是不必要的,它不会参加DI。