2017-07-12 28 views
0

我试图在我的应用程序使用angular-ui-router,但在导入

Error: [$injector:modulerr] Failed to instantiate module uiRouter due to: 
Error: [$injector:nomod] Module 'uiRouter' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. 

这里的时候,我有问题是我main.js

import angular from 'angular'; 
import uiRouter from 'angular-ui-router'; 

(function() { 
    function config($locationProvider, $stateProvider) { 
     $locationProvider 
      .html5Mode({ 
      enabled: true, 
      requireBase: false 
     }); 

    } 
    angular 
     .module('myApp', ['uiRouter']) 
     .config(config); 

})(); 

index.html文件的应用程序宣布 <html lang="en" ng-app="myApp"> 我已经通过NPM安装两个包

[email protected] /Users/.../myApp 
├── [email protected] 
├─┬ [email protected] 
│ └── @uirouter/[email protected] 

不知道,如果是相关的,但也宣告它在package.json

"dependencies": { 
    "angular": "^1.6.5", 
    "angular-ui-router": "^1.0.3", 

在过去,我一直通过<script>标签进口角图书馆index.html所以我不熟悉进口的包。

另外,我读的是ui-router作为范围包提供:https://ui-router.github.io/blog/uirouter-scoped-packages/但不知道它是否与此处相关(我也导入了此链接中描述的包,但似乎没有帮助)。

问题为什么不是angular-ui-router被识别?

+0

你可以提供一个你的代码plunker。你确定包含angular-ui-router吗 – user93

+0

你是否在脚本标签中包含了'angular-ui-router'在你的html里面? –

回答

0

好吧我得到了这个工作,但它不是我最初的预期。

我改变main.js阅读

import angular from 'angular'; 
import uiRouter from 'angular-ui-router'; 

(function() { 
    function config($stateProvider, $urlRouterProvider) { 
     /* $locationProvider //ignore 
      .html5Mode({ 
      enabled: true, 
      requireBase: false 
     }); */ 

    } 
    angular 
     .module('myApp', ['ui.router']) 
     .config(config); 

})(); 

要解释一下:我在这里使用两个ES6导入功能和以及角度依赖性注入。我通过uiRouter从'angular-ui-router'导入,以便main.js可以访问这个ui路由器对象。然后我在Angular依赖注入中使用ui.router,因为这是Angular中ui路由器的内部名称。 ngRoute和ui路由器的提供者名称也不同(原始代码中的$locationProvider与此处的$urlRouterProvider)。我不使用<script>标签导入一个Angular库。