如何将服务注入路由器,以便其结果(json)在整个应用程序中都可用?

路由器:

export default ['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider, MainController, ConfigService) {

$stateProvider.state('home', {
    url: '/',
    controller: 'MainController',
    resolve: {

      /* @ngInject */
      someResolve: (ConfigService) =>
        ConfigService.config()
    }
});

$urlRouterProvider.otherwise('/');
}];


服务:

export default function ConfigService() {
return ['$http', function($http) {

    this.config = function() {
        return $http({
            url: '../json/config.json',
            method: 'GET'
        }).then(function(result) {
            console.log(result);
        });
    };
}];
}


主控制器

export default function MainController () {
return ['$scope','$location','ConfigService','$window', function($scope, $location, ConfigService, $window) {

    ConfigService.config();

}];
}

最佳答案

MainController和ConfigService没有指定任何提供程序...您的声明应类似于

export default ['$stateProvider', '$urlRouterProvider','MainController', 'ConfigService', function($stateProvider, $urlRouterProvider, MainController, ConfigService) {}

09-20 18:29