我遵循RequireJS网站上的循环参考说明,但它没有';行不通
我有一个简单的例子,我相信它遵循RequestJS文档中的说明:但是,它似乎不起作用。我把我的文件放在下面,还有一个zip文件放在dropbox里:有人能告诉我发生了什么事吗我遵循RequireJS网站上的循环参考说明,但它没有';行不通,requirejs,circular-dependency,Requirejs,Circular Dependency,我有一个简单的例子,我相信它遵循RequestJS文档中的说明:但是,它似乎不起作用。我把我的文件放在下面,还有一个zip文件放在dropbox里:有人能告诉我发生了什么事吗 index.html: <!doctype html> <html> <head> <script data-main="js/app" src="js/require.js" type="text/javascript"></script> </h
index.html:
<!doctype html>
<html>
<head>
<script data-main="js/app" src="js/require.js" type="text/javascript"></script>
</head>
<body>
Loaded.
</body>
</html>
app.js:
requirejs.config({
paths: {
'module1': 'module1',
'module2': 'module2'
}
});
requirejs(['main'], function(main) {
main.start();
});
main.js:
define(['module1'], function (module1) {
var start = function() {
console.log('In main.js. Before initializeValues.');
module1.doOperation();
}
return {
start: start
};
});
module1.js:
define(['require', 'module2'],
function (require, module2) {
var doOperation = function() {
console.log('Start of doOperation function');
require('module2').someFunc();
};
var anotherFunc = function() {
console.log('In anotherFunc.');
};
return {
doOperation: doOperation,
anotherFunc: anotherFunc
};
});
module2.js:
require(['require', 'module1'], function(require, module1) {
var someFunc = function() {
console.log('In someFunc.');
require('module1').anotherFunc();
};
return {
someFunc: someFunc
};
});
require.js:
/** vim: et:ts=4:sw=4:sts=4
* @license RequireJS 2.0.4 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details
*/
/*jslint regexp: true, nomen: true */
/*global window, navigator, document, importScripts, jQuery, setTimeout, opera */
var requirejs, require, define;
(function (global) {
'use strict';
var version = '2.0.4',
commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg,
cjsRequireRegExp = /[^.]\s*require\s*\(\s*["']([^'"\s]+)["']\s*\)/g,
jsSuffixRegExp = /\.js$/,
: :
index.html:
加载。
app.js:
requirejs.config({
路径:{
“模块1”:“模块1”,
“模块2”:“模块2”
}
});
requirejs(['main'],函数(main){
main.start();
});
main.js:
定义(['module1'],函数(module1){
var start=function(){
log('initializeValue'之前的main.js.');
模块1.doOperation();
}
返回{
开始:开始
};
});
模块1.js:
定义(['require','module2'],
功能(要求,模块2){
var doOperation=函数(){
console.log(“开始操作功能”);
require('module2')。someFunc();
};
var anotherFunc=函数(){
console.log('In anotherFunc');
};
返回{
doOperation:doOperation,
anotherFunc:anotherFunc
};
});
module2.js:
require(['require',module1'],函数(require,module1){
var someFunc=函数(){
log('In someFunc');
require('module1')。anotherFunc();
};
返回{
someFunc:someFunc
};
});
require.js:
/**vim:et:ts=4:sw=4:sts=4
*@许可证2.0.4版权(C)2010-2012,Dojo基金会保留所有权利。
*可通过麻省理工学院或新的BSD许可证获得。
*见:http://github.com/jrburke/requirejs 详情
*/
/*jslint regexp:true,nomen:true*/
/*全局窗口、导航器、文档、importScripts、jQuery、setTimeout、opera*/
var requirejs,require,define;
(职能(全球){
"严格使用",;
变量版本='2.0.4',
commentRegExp=/(\/\*([\s\s]*?)\*\/\/\;([^:]\^^)\/\/(.*)/mg,
cjsRequireRegExp=/[^.]\s*需要\s*\(\s*[“”]([^''\s]+)[“]\s*\)/g,
jsSuffixRegExp=/\.js$/,,
: :
我在控制台中得到消息:UncaughtTypeError:无法读取未定义的…module1.js:7的属性'someFunc'
当我不明白RequireJS在做什么时,很难向前推进。有人能解释一下吗?在
module2.js
中,您有:
require(['require', 'module1'], function(require, module1) {
您要使用:
define(['require', 'module1'], function(require, module1) {
您需要使用
define
来定义模块。是的,这就解决了问题。谢谢!现在我将回到原来的问题,我简化为这个问题,看看我是否能让它工作。我试图投票支持您的答案,但它说我的声誉太差了!