将Handsontable与RequireJS一起使用

将Handsontable与RequireJS一起使用,requirejs,handsontable,Requirejs,Handsontable,当使用requirejs填充Handsontable时,我不断得到以下错误和堆栈跟踪 Uncaught TypeError: undefined is not a function VM18361 handsontable.full.js:20729 unformatNumeral VM18361 handsontable.full.js:21325 numeral.fn.Numeral.unformat

当使用requirejs填充Handsontable时,我不断得到以下错误和堆栈跟踪

Uncaught TypeError: undefined is not a function VM18361 handsontable.full.js:20729 
unformatNumeral                                 VM18361 handsontable.full.js:21325
numeral.fn.Numeral.unformat                     VM18361 handsontable.full.js:21325
numeral                                         VM18361 handsontable.full.js:21037
即使使用来自的示例,也会发生这种情况

我的requirejs配置和使用handsontable的模块如下所示

require.config({                                                                                                                                         
 paths: {
   handsontable : '/js/dependencies/handsontable.full'                                                                               
},                                                                              
shim: {                                                                            
  'handsontable': {                                                             
    deps: ['jquery'],                                                           
    exports: 'Handsontable'                                                     
  }
}                                                                         

define(['handsontable'], function(Handsontable) { 
  var data = [
    ['', 'Maserati', 'Mazda', 'Mercedes', 'Mini', 'Mitsubishi'],
    ['2009', 0, 2941, 4303, 354, 5814],
    ['2010', 3, 2905, 2867, 412, 5284],
    ['2011', 4, 2517, 4822, 552, 6127],
    ['2012', 2, 2422, 5399, 776, 4151]
  ];

  var container = document.getElementById('example');

  var config = {
    data: data,
    minSpareRows: 1,
    colHeaders: true,
    contextMenu: true
  };

  var hot = new Handsontable(container, config); 
});
还有其他人遇到过这个问题吗

目前,我能看到的唯一解决方案是将handsontable作为一个全局对象(绕过了管理依赖项的整个需求)

我希望有更好的解决办法


谢谢

我认为这里的问题是您使用的是完整版本的Handsontable,其中包括依赖项,如numeric.js。由于某些依赖项与AMD兼容,即调用了define(),因此最终会引用numeric.js,而不是Handsontable

要正确使用它,只需使用裸发行版文件hansontable.js,并包含该版本hansontable所需的所有依赖项。大概是这样的:

require.config({                                                                                                                                         
  paths: {
    handsontable : '/js/dependencies/handsontable'                                                                               
  },                                                                              
  shim: {                                                                            
    'handsontable': {                                                             
      deps: ['moment', 'pikaday', 'zeroclipboard'],                                                           
      exports: 'Handsontable'                                                     
    }
  }
})

我不确定您使用的是哪个版本的Handsontable,当前版本为0.20.3,取决于moment、pikaday和zeroclipboard。有关更多信息,请参阅。

您能分享给出错误的代码吗?这看起来像是与requireJS一起使用时可手持内部API的一些问题,您应该报告它们。。您的代码是正确的,您给出的错误提到了handsontable中的代码,这意味着您已经从RequireJS获得了正确的对象。谢谢,在我确定自己的代码中没有出现问题之前,我不想报告问题。您能记录容器和配置的内容吗?这是您所期望的吗?容器和配置看起来都很正常。在我开始在项目中使用require.js之前,几乎相同的代码运行良好。