2017-05-19 136 views
1

我买了metronic管理模板并试图在角度2中使用它,但是我有两个JS文件出错:app.jslayout.js不能从一个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() {/* ... */} 
+0

如果console.log正常工作,那么您的浏览器正在欺骗您:v –

+0

当然,它会失败,因为'test.getResponsiveBreakpoint'还不是一个函数。它不会像当前的布局结构一样。 – dfsq

+0

你是如何加载js文件的? – PierreDuc

回答

1

的功能Layout删除self invoking。 即, 代替此

var Layout = function() { 
    ... 
    //Your Code... 
}(); 

使用此的,

var Layout = function() { 
    ... 
    //Your Code... 
}; 

UPDATE:

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; 
     } 
    } 
}(); 

layout.js

var Layout = function() { 
    var resBreakpointMd = test.getResponsiveBreakpoint('xs'); 
    return{ 
     init:function(){ 
      return resBreakpointMd; 
     } 
     }  
}; 

$(document).ready(function() { 
    console.log(Layout().init()); 
}); 
+0

是的,我知道,app.js首先加载,但错误仍然存​​在 –

+0

我改变了它,并调用它在 '$(文件)。就绪(函数(){ 布局(); });' 但同样的错误 –

+0

你删除自身调用了'test'功能井?如果是这样,不要删除'测试'功能。 – bhadri

相关问题