Javascript 数据表:未捕获的类型错误:无法读取未定义的属性“默认值”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26165263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 22:26:34  来源:igfitidea点击:

DataTables: Uncaught TypeError: Cannot read property 'defaults' of undefined

javascripttwitter-bootstrapdatatables

提问by Ryan Kohn

When using the Bootstrap integrationfor DataTables, I see the following error in the console:

对 DataTables使用Bootstrap 集成时,我在控制台中看到以下错误:

Uncaught TypeError: Cannot read property 'defaults' of undefined (dataTables.bootstrap.js:20)

This causes the pagination controls to not have styles on them.

这会导致分页控件上没有样式。

I can see that in the factory initialization, the following code needs to run:

可以看到在工厂初始化中,需要运行如下代码:

factory( jQuery, jQuery.fn.dataTable );

However, jQuery.fn.dataTableis returning undefined.

然而,jQuery.fn.dataTable正在回归undefined

回答by Ryan Kohn

The problem is that dataTableis not defined at the point you are calling this method.

问题是dataTable在您调用此方法时未定义。

Ensure that you are loading the .jsfiles in the correct order:

确保.js以正确的顺序加载文件:

<script src="/Scripts/jquery.dataTables.js"></script>
<script src="/Scripts/dataTables.bootstrap.js"></script>

回答by Alan Elias

I got the same error, I'm using laravel 5.4 with webpack, here package.json before:

我遇到了同样的错误,我在 webpack 中使用 laravel 5.4,这里是 package.json 之前:

{
  ...
  ...
  "devDependencies": {
    "jquery": "^1.12.4",
    ...
    ...
  },
  "dependencies": {
    "datatables.net": "^2.1.1",
    ...
    ...
  }
}

I had to move jqueryand datatables.netnpm packages under one of these "dependencies": {}or "devDependencies": {}in package.jsonand the error disappeared, after:

我不得不搬到jquerydatatables.net根据其中的一个NPM包"dependencies": {}"devDependencies": {}package.json和错误消失后:

{
  ...
  ...
  "devDependencies": {
    "jquery": "^1.12.4",
    "datatables.net": "^2.1.1",
    ...
    ...
  }
}

I hope that helps!

我希望这有帮助!

回答by Eugene Oca

var datatable_jquery_script = document.createElement("script");
datatable_jquery_script.src = "vendor/datatables/jquery.dataTables.min.js";
document.body.appendChild(datatable_jquery_script);
setTimeout(function(){
    var datatable_bootstrap_script = document.createElement("script");
    datatable_bootstrap_script.src = "vendor/datatables/dataTables.bootstrap4.min.js";
    document.body.appendChild(datatable_bootstrap_script);
},100);

I used setTimeOut to make sure datatables.min.js loads first. I inspected the waterfall loading of each, bootstrap4.min.js always loads first.

我使用 setTimeOut 来确保首先加载 datatables.min.js。我检查了每个的瀑布加载,bootstrap4.min.js 总是首先加载。