DataTables+RequireJS:无法读取属性'默认'的未定义
DataTables+RequireJS: Cannot read property 'defaults' of undefined
我已经下载了DataTables
的完整包及其所有模块,因为它无法通过CDN URL访问:
https://www.datatables.net/download/(选择所有选项)
我正试图让它与RequireJS
一起运行,整个DataTables
包都使用相同的依赖系统,所以它不应该失败。
JSFiddle(为JSFiddle:http://jsfiddle.net/42ucpwee/1/
我的配置导致此错误:
datatables.js:93165 Uncaught TypeError: Cannot read property 'defaults' of undefined
datatables.js:93161-93171:
var DataTable = $.fn.dataTable;
/* Set the defaults for DataTables initialisation */
$.extend( true, DataTable.defaults, {
dom:
"<'row'<'col-sm-6'l><'col-sm-6'f>>" +
"<'row'<'col-sm-12'tr>>" +
"<'row'<'col-sm-5'i><'col-sm-7'p>>",
renderer: 'bootstrap'
} );
造成此错误的原因是什么?是我遗漏了什么,还是我的配置错误?
script.js:
define(['jquery','datatables.net'], function($) {
$('#example').DataTable();
});
main.js:
requirejs.config({
baseUrl: "lib",
paths: {
'jquery': '//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min',
'bootstrap': '//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min',
'datatables.net': 'DataTables/datatables',
'script': '../js/script'
},
shim: {
'bootstrap': {
deps: ['jquery']
},
'jquery': {
exports: '$'
},
'datatables.net': {
deps: ['bootstrap','jquery']
},
'script': {
deps: ['jquery','datatables.net']
}
}
});
requirejs(['script']);
index.html:
<html>
<head>
<link rel="stylesheet" href="https://cdn.datatables.net/s/bs-3.3.5/jszip-2.5.0,pdfmake-0.1.18,dt-1.10.10,af-2.1.0,b-1.1.0,b-colvis-1.1.0,b-flash-1.1.0,b-html5-1.1.0,b-print-1.1.0,cr-1.3.0,fc-3.2.0,fh-3.1.0,kt-2.1.0,r-2.0.0,rr-1.1.0,sc-1.4.0,se-1.1.0/datatables.min.css" type="text/css" />
<script type="text/javascript" src="js/require.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
<table id="example" class="display" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
</tr>
</thead>
<tbody>
<tr>
<td>Tiger Nixon</td>
<td>System Architect</td>
</tr>
</tbody>
</table>
</body>
</html>
一个可以接受的答案是下载单个模块(而不是单个文件选项)并使用以下脚本,这似乎比一次包含所有模块造成的问题更少:
http://jsfiddle.net/42ucpwee/2/
requirejs.config({
appDir: ".",
baseUrl: "js",
paths: {
'jquery': '//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min',
'bootstrap': '//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min',
'datatables' : 'jquery.dataTables.min',
'datatables-bootstrap' : 'dataTables.bootstrap',
},
shim : {
'jquery' : {
exports : 'jquery'
},
'bootstrap' : {
deps : [ 'jquery' ],
exports : 'Bootstrap'
},
'datatables' : [ 'jquery' ],
'datatables-bootstrap' : [ 'datatables' ],
}
});
require([
'jquery',
'datatables-bootstrap'
], function ($) {
$('#example').DataTable();
});
相关文章:
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- $window.ga在AngularJS事件中未定义
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 为什么我的模型中的默认属性返回未定义
- 复选框默认值“选中”,使用 jquery 1.9.1 给出未定义的值
- 未定义 ng 检查的默认值
- 谷歌分析未定义默认值
- 当未定义默认导出时,从“模块”导入模块是什么,为什么它与将 * 导入为模块不同
- 为具有未定义值的嵌套对象设置默认值的最简单方法
- 当挖空可观察未定义或禁用 JS 时显示默认值
- Javascript:使用变量,或者如果它's未定义,为默认字符串
- DataTables:未捕获类型错误:无法读取属性'默认'的未定义
- DataTables+RequireJS:无法读取属性'默认'的未定义
- Dojo lang.replace - 如何提供默认值,而不是“未定义”
- 绑定observableArray中未定义的字段或提供默认值
- 如果绑定为空/未定义(带过滤器),Angularjs模板默认值