0
我有一個button.My HTML文件加載main.js一個html文件,如下所示:參考錯誤:menu.show未定義
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My Account</title>
<!-- RequireJS bootstrap file -->
<script data-main="lib/main" src="lib/require.js"></script>
</head>
<body>
<p>First name: <input data-bind="value:firstName"/></p>
<p>Last name: <input data-bind="value:lastName"/></p>
<p>Full name: <strong data-bind="text: fullName"></strong></p>
<button data-bind="click: capitalizeLastName">Go caps</button>
</body>
</html>
Main.js加載另一個應用程序的js稱爲menu.js其中有一個叫show的功能。 在按鈕的點擊,我調用此函數顯示如下:
Main.js被定義爲:
requirejs.config({
//Path mappings for the logical module names
paths: {
'knockout': 'jslibs/knockout-2.3.0',
'jquery': 'jslibs/jquery-1.11.0',
'menu':'jslibs/menu'
},
// Shim configurations for modules that do not expose AMD
shim: {
'jquery': {
exports: ['jQuery', '$']
}
},
});
require(['jquery','knockout','menu'],
function($,ko) {
function AppViewModel() {
var self = this;
self.firstName = ko.observable("");
self.lastName = ko.observable("");
self.fullName = ko.computed(function(){
return this.firstName() +" "+this.lastName();
},this);
this.capitalizeLastName = function() {
menu.show();
};
};
vm= new AppViewModel();
$(document).ready(function() {
ko.applyBindings(vm);
});});
menu.js被定義爲:
define(['jquery','knockout'],function($,ko){
var add = {
show: function(id) {
alert('inside show');
}
};
return add;
});
在按鈕單擊,我得到錯誤:
ReferenceError:菜單未定義
menu.show();
任何指針?我檢查了路徑,所有路徑都是有效的。
感謝 TANU