Javascript 模块不可用、拼写错误或忘记加载(但我没有)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31394850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:38:55  来源:igfitidea点击:

Module is not available, misspelled or forgot to load (but I didn't)

javascriptjsonangularjsmoduleangularjs-injector

提问by Ian McCleary

I am fairly new to angular and using it with JSON api files. TO test, I am trying to use the free github api (my names for functions are for a different json api that i will be working with later). I just wanted to see if my functions were working with console.log(), but i receive this error in the console.

我对 angular 和将它与 JSON api 文件一起使用还很陌生。为了进行测试,我正在尝试使用免费的 github api(我的函数名称用于我稍后将使用的不同 json api)。我只是想看看我的函数是否正在使用console.log(),但我在控制台中收到了这个错误。

Uncaught Error: [$injector:modulerr] Failed to instantiate module MesaViewer due to: Error: [$injector:nomod] Module 'MesaViewer' 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.

未捕获的错误:[$injector:modulerr] 无法实例化模块 MesaViewer,原因是:错误:[$injector:nomod] 模块“MesaViewer”不可用!您拼错了模块名称或忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。

I have spelled MesaViewerthe exact same in both, and dependencies are seen in the second line!

MesaViewer在两者中拼写完全相同,并且在第二行中可以看到依赖关系!

var app = angular.module("MesaViewer");
var MainController = function($scope, $location, $http, $routeParams) {

What did I do wrong? Here is my plunk: http://plnkr.co/edit/sZPaFbzbOB6AmVCLL1vq

我做错了什么?这是我的 plunk:http: //plnkr.co/edit/sZPaFbzbOB6AmVCLL1vq

回答by Matt Herbstritt

It should be

它应该是

var app = angular.module("MesaViewer", []);

This is the syntax you need to define a module and you need the array to show it has no dependencies.

这是定义模块所需的语法,并且需要数组来显示它没有依赖项。

You use the

您使用

angular.module("MesaViewer");

syntax when you are referencing a module you've already defined.

引用已定义的模块时的语法。

回答by charlietfl

You are improperly declaring your main module, it requires a second dependencies array argument when creating a module, otherwise it is a reference to an existing module

您未正确声明主模块,创建模块时需要第二个依赖项数组参数,否则它是对现有模块的引用

Change:

改变:

var app = angular.module("MesaViewer");

To:

到:

var app = angular.module("MesaViewer",[]);

Working Version

Working Version

回答by Sruthi Poddutur

I had the same error and fixed it. It turned out to be a silly reason.

我有同样的错误并修复了它。结果证明这是一个愚蠢的理由。

This was the culprit: <script src="app.js"/>

Fix: <script src="app.js"></script>

这是罪魁祸首: <script src="app.js"/>

使固定: <script src="app.js"></script>

Make sure your script tag is ended properly!

确保您的脚本标签正确结束!

回答by Slicedbread

I found this question when I got the same error for a different reason.

当我由于不同的原因遇到相同的错误时发现了这个问题。

My issue was that my Gulp hadn't picked up on the fact that I had declared a new module and I needed to manually re-run Gulp.

我的问题是我的 Gulp 没有意识到我已经声明了一个新模块并且我需要手动重新运行 Gulp。

回答by Roberth Godoy

I have the same problem, but I resolved adding jquery.min.js before angular.min.js.

我有同样的问题,但我解决了在 angular.min.js 之前添加 jquery.min.js 的问题。

回答by sandeep kumar

make sure that you insert your module and controller in your index.html first. then use this code in your module var app = angular.module("MesaViewer", []);

确保首先在 index.html 中插入模块和控制器。然后在您的模块中使用此代码 var app = angular.module("MesaViewer", []);

回答by ArifMustafa

Using AngularJS1.6.9+

使用AngularJS1.6.9+

There is one more incident, it also happen when you declare variable name different of module name.

还有一件事,当你声明变量名与模块名不同时也会发生这种情况。

var indexPageApp = angular.module('indexApp', []);

to get rid of this error,

为了摆脱这个错误,

Error: [$injector:nomod] Module 'indexPageApp' 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.

错误:[$injector:nomod] 模块“indexPageApp”不可用!您拼错了模块名称或忘记加载它。如果注册模块,请确保将依赖项指定为第二个参数。

change the module name similar to var declared name or vice versa -

更改类似于 var 声明名称的模块名称,反之亦然 -

var indexPageApp = angular.module('indexPageApp', []);

回答by noushad mohammed

I had the same error but i resolved it, it was a syntax error in the AngularJS provider

我有同样的错误,但我解决了它,这是 AngularJS 提供程序中的语法错误

回答by Todd Hale

I got this error when my service declaration was inside a non-invoked function (IIFE). The last line below did NOT have the extra () to run and define the service.

当我的服务声明位于非调用函数 (IIFE) 内时,我收到此错误。下面的最后一行没有额外的 () 来运行和定义服务。

(function() {
    "use strict";

    angular.module("reviewService", [])
        .service("reviewService", reviewService);

    function reviewService($http, $q, $log) {
        //
    }
}());