我购买了 metronic 管理模板并尝试在 Angular 2 中使用它,但两个 JS 文件出现错误:app.js
和 layout.js
。
app.js
内容:
var test = function () {
/* ... */
return {
init: function(){ /* ... */ },
getResponsiveBreakpoint: function(size) {
// bootstrap responsive breakpoints
var sizes = {
'xs': 480, // extra small
'sm': 768, // small
'md': 992, // medium
'lg': 1200 // large
};
return sizes[size] ? sizes[size] : 0;
}
}
}();
jQuery(document).ready(function() {
test.init(); // init metronic core componets
});
layout.js
内容:
var Layout = function () {
var resBreakpointMd = test.getResponsiveBreakpoint('md');
/* ... */
return {
init: function() { /* ... */ }
}
}();
$(document).ready(function() {
Layout.init(); // init metronic core componets
});
layout.js
返回此错误:
test.getResponsiveBreakpoint is not a function
但是如果我在布局函数之外编写 test.getResponsiveBreakpoint('md');
,它就可以工作。
console.log(test.getResponsiveBreakpoint('md'));
var Layout = function() {/* ... */}
最佳答案
删除函数Layout
的自调用
。
IE,
而不是这个
var Layout = function () {
...
//Your Code...
}();
使用这个,
var Layout = function () {
...
//Your Code...
};
更新:
app.js
var test = function () {
return {
init:function(){
return 100;
},
getResponsiveBreakpoint: function (size) {
var sizes = {
'xs': 480, // extra small
'sm': 768, // small
'md': 992, // medium
'lg': 1200 // large
};
return sizes[size] ? sizes[size] : 0;
}
}
}();
布局.js
var Layout = function () {
var resBreakpointMd = test.getResponsiveBreakpoint('xs');
return{
init:function(){
return resBreakpointMd;
}
}
};
$(document).ready(function() {
console.log(Layout().init());
});
关于javascript - 无法从一个js调用函数到第二个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44066147/