由于以下原因无法实例化模块appRoutes:[$ injector:unpr]未知提供者:$ routeProvider

时间:2017-11-16 12:08:27

标签: javascript angularjs

我已经在index.html页面中添加了angular-route.js

<script type = "text/javascript" src = "bower_components/angular/angular.js"></script>
<script type="text/javascript" src="bower_components/angular-route/angular-route.js"></script>
<script type="text/javascript" src="bower_components/oclazyload/dist/ocLazyLoad.min.js"></script>
<script type = "text/javascript" src = "public/app/appRoutes.js"></script>

appRoutes.js:

angular.module('appRoutes', ['ngRoute', 'oc.lazyLoad']).config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {

  $routeProvider

  .when('/', {
    templateUrl: 'app/components/in-progress/in-progress.template.html',
    controller: 'inProgressController',
    resolve: {
      lazy: ['$ocLazyLoad', function($ocLazyLoad) {
        return $ocLazyLoad.load({files: ['app/components/in-progress/in-progress.component.js']});
      }]
    }
  })
.... different routes
$locationProvider.html5Mode({
    enabled: true,
    requireBase: false
  });

}]);

我不明白,为什么由于$routeProvider导致失败,即使我采取了所有可能的预防措施。

我也在我的主应用程序中添加了ng-route

Angular版本:1.5

1 个答案:

答案 0 :(得分:0)

您的代码缺少'ngInject'。这是阵列注入后通常需要的。尝试添加它......

   angular.module('appRoutes', ['ngRoute', 'oc.lazyLoad']).config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
'ngInject';

      $routeProvider

      .when('/', {
        templateUrl: 'app/components/in-progress/in-progress.template.html',
        controller: 'inProgressController',
        resolve: {
          lazy: ['$ocLazyLoad', function($ocLazyLoad) {
            return $ocLazyLoad.load({files: ['app/components/in-progress/in-progress.component.js']});
          }]
        }
      })
    .... different routes
    $locationProvider.html5Mode({
        enabled: true,
        requireBase: false
      });

    }]);