繁体   English   中英

AngularJS:[$ injector:nomod]模块'uiRouter'不可用

[英]AngularJS: [$injector:nomod] Module 'uiRouter' is not available

我试图在我的应用程序中使用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安装了这两个软件包

myApp@1.0.0 /Users/.../myApp
├── angular@1.6.5
├─┬ angular-ui-router@1.0.3
│ └── @uirouter/core@5.0.3

不知道它是否相关,但也在package.json声明了它

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

过去,我总是通过index.html <script>标记导入角度库,因此对作为包导入不熟悉。

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

问题为什么无法识别angular-ui-router

好的,我让它起作用了,但这不是我最初的期望。

我将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导入功能和Angular依赖注入。 我正在通过uiRouter从“ angular-ui-router”导入,以便main.js可以访问此ui路由器对象。 然后我在Angular依赖项注入中使用ui.router,因为这是Angular中ui路由器的内部名称。 ngRoute和ui-router的提供程序名称也不同(原始代码中的$locationProvider与此处的$urlRouterProvider )。

我根本不使用<script>标记来导入Angular库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM