gpt4 book ai didi

javascript - RequireJS + Angular : Undefined app. 回调不触发

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:31:34 26 4
gpt4 key购买 nike

我用这个简单的代码得到了 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);
});

如您所见,我尝试在 require 回调中获取应用程序。但我得到的都是未定义的。这是我在 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;
});

那么问题来了:main.js 回调中未定义的函数参数“mainApp”似乎是合乎逻辑的,因为 app.js 中的 console.log() 不会射击。有人可以告诉我它有什么问题吗?

最佳答案

只需删除 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

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.

关于javascript - RequireJS + Angular : Undefined app. 回调不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27482394/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com