热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

RequireJS+Angular:Undefinedapp.Callbackdoesn'tshoot

Igotmain.jswiththissimplecode:我用这个简单的代码得到了main.js:usestrickt;require.config({paths

I got main.js with this simple code:

我用这个简单的代码得到了main.js:

'use strickt';

require.config({

  paths: {
    'angular'        : 'libs/angular'          ,
    'angular-router' : 'libs/angular-route'    ,
  },
  shim : {
    'angular' : {
      exports : 'angular'
    },
    'angular-router' : {
      deps: ['angular']
    }
  }

});

require(['app'], function (mainApp) {
  console.log(mainApp);
});

As you can see, I try to fetch app inside require callback. But all I got its undefined. Here what I got inside app.js file:

如您所见,我尝试在需要回调中获取应用程序。但我得到的都是未定义的。这是我在app.js文件中得到的:

define('mainApp', ['angular', 'angular-route'], function (angular) {
  var app = angular.module('mainApp', ['ngRoute']);
  console.log('should be fired from app.js');
  return app;
});

So the question: Function argument 'mainApp' as undefined inside main.js callback seems logical because console.log() inside app.js doesnt shoot. Can somebody tell me what is wrong with it?

所以问题:函数参数'mainApp'在main.js回调中未定义似乎是合乎逻辑的,因为app.js中的console.log()不会发生。有人能告诉我它有什么问题吗?

1 个解决方案

#1


1  

Just remove module name in app.js file (or change it to 'app'). You app.js file will look like:

只需删除app.js文件中的模块名称(或将其更改为“app”)。你的app.js文件看起来像:

define(['angular', 'angular-route'], function (angular) {
  var app = angular.module('mainApp', ['ngRoute']);
  console.log('should be fired from app.js');
  return app;
});

http://requirejs.org/docs/api.html#modulename

http://requirejs.org/docs/api.html#modulename

You can explicitly name modules yourself, but it makes the modules less portable -- if you move the file to another directory you will need to change the name. It is normally best to avoid coding in a name for the module and just let the optimization tool burn in the module names. The optimization tool needs to add the names so that more than one module can be bundled in a file, to allow for faster loading in the browser.

您可以自己明确命名模块,但它会使模块的可移植性降低 - 如果将文件移动到另一个目录,则需要更改名称。通常最好避免使用模块名称进行编码,只需让优化工具在模块名称中进行刻录。优化工具需要添加名称,以便可以在文件中捆绑多个模块,以便在浏览器中加快加载速度。


推荐阅读
author-avatar
寡妇毒_393
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有