无法从另一个 dojo 模块调用 dojo 模块中的函数
unable to call function in dojo module from another dojo module
当我尝试调用 dojo 模块中的函数时,出现对象不支持 属性 或方法错误。我有一个主页和两个模块。我从主页调用第一个模块并且它工作,我从第一个模块调用第二个模块并且它工作,但是当我尝试从第二个模块调用第一个模块时出现错误。这是我的代码:
主页:
<!DOCTYPE html>
<html >
<head>
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
<link rel="stylesheet" href="http://js.arcgis.com/3.10/js/dojo/dijit/themes/claro/claro.css">
<link rel="stylesheet" href="http://js.arcgis.com/3.10/js/esri/css/esri.css">
<script>
var dojoConfig = {
parseOnLoad:true,
async: true,
isDebug:true,
packages: [
{name: "Scripts", location: location.pathname.replace(/\/[^/]+$/, "") + "/Scripts"},
]
};
</script>
<script> src="http://js.arcgis.com/3.10/"></script>
<script>
require(["Scripts/Mod1", "Scripts/Mod2"],
function (Mod1, Mod2) {
Mod1.M1Method("call from main page");//works great
});
</script>
</head>
<body class="claro">
<div>look here you</div>
</body>
</html>
Mod规则 1:
define(["Scripts/Mod2"],
function (Mod2) {
return {
M1Method: function (msg) {
alert(msg);
Mod2.M2Method("call from Mod1");//works great
},
M1Method2: function (msg) {
alert(msg);
}
}
});
Mod规则 2:
define(["Scripts/Mod1"],
function (Mod1) {
return {
M2Method: function (msg) {
alert(msg);
Mod1.M1Method2("call from Mod2"); //JavaScript runtime error: Object doesn't support property or method 'M1Method2'
}
}
});
如何从 Mod 2 拨打 Mod1 电话?
谢谢
您正在尝试进行循环依赖。在模块 2 中,使用 require 语句。尝试这样的事情:
define(["require"],
function (require) {
return {
M2Method: function (msg) {
alert(msg);
try {
require(["Scripts/Mod1"], function(Mod1) {
Mod1.M1Method2("call from Mod2");
});
} catch (dohObj) {
alert('Doh!, this failed. Stupid answer: ' + dohObj.message);
}
}
}
}
);
您的 html 代码有一个拼写错误,您在包含 dojo.js 时过早地包含了 'script' 标签。应该是这样的
<script type="text/javascript" src="http://js.arcgis.com/3.10/"></script>
您需要先修复该文件才能正确包含 dojo 和 'require()'。
当我尝试调用 dojo 模块中的函数时,出现对象不支持 属性 或方法错误。我有一个主页和两个模块。我从主页调用第一个模块并且它工作,我从第一个模块调用第二个模块并且它工作,但是当我尝试从第二个模块调用第一个模块时出现错误。这是我的代码:
主页:
<!DOCTYPE html>
<html >
<head>
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
<link rel="stylesheet" href="http://js.arcgis.com/3.10/js/dojo/dijit/themes/claro/claro.css">
<link rel="stylesheet" href="http://js.arcgis.com/3.10/js/esri/css/esri.css">
<script>
var dojoConfig = {
parseOnLoad:true,
async: true,
isDebug:true,
packages: [
{name: "Scripts", location: location.pathname.replace(/\/[^/]+$/, "") + "/Scripts"},
]
};
</script>
<script> src="http://js.arcgis.com/3.10/"></script>
<script>
require(["Scripts/Mod1", "Scripts/Mod2"],
function (Mod1, Mod2) {
Mod1.M1Method("call from main page");//works great
});
</script>
</head>
<body class="claro">
<div>look here you</div>
</body>
</html>
Mod规则 1:
define(["Scripts/Mod2"],
function (Mod2) {
return {
M1Method: function (msg) {
alert(msg);
Mod2.M2Method("call from Mod1");//works great
},
M1Method2: function (msg) {
alert(msg);
}
}
});
Mod规则 2:
define(["Scripts/Mod1"],
function (Mod1) {
return {
M2Method: function (msg) {
alert(msg);
Mod1.M1Method2("call from Mod2"); //JavaScript runtime error: Object doesn't support property or method 'M1Method2'
}
}
});
如何从 Mod 2 拨打 Mod1 电话?
谢谢
您正在尝试进行循环依赖。在模块 2 中,使用 require 语句。尝试这样的事情:
define(["require"],
function (require) {
return {
M2Method: function (msg) {
alert(msg);
try {
require(["Scripts/Mod1"], function(Mod1) {
Mod1.M1Method2("call from Mod2");
});
} catch (dohObj) {
alert('Doh!, this failed. Stupid answer: ' + dohObj.message);
}
}
}
}
);
您的 html 代码有一个拼写错误,您在包含 dojo.js 时过早地包含了 'script' 标签。应该是这样的
<script type="text/javascript" src="http://js.arcgis.com/3.10/"></script>
您需要先修复该文件才能正确包含 dojo 和 'require()'。