对于Angular 4,使用angular-datatables时DataTable不是函数TypeError
我在代码中使用angular-datatables时出现TypeError。在该指令对于Angular 4,使用angular-datatables时DataTable不是函数TypeError
ERROR TypeError: $(...).DataTable is not a function
at angular-datatables.directive.js:38
代码是:
DataTableDirective.prototype.displayTable = function() {
var _this = this;
this.dtInstance = new Promise(function (resolve, reject) {
Promise.resolve(_this.dtOptions).then(function (dtOptions) {
// Using setTimeout as a "hack" to be "part" of NgZone
setTimeout(function() {
//Error in this line
var dt = $(_this.el.nativeElement).DataTable(dtOptions);
resolve(dt);
});
});
});
};
这里有什么问题吗?我通过以下方式安装了库和依赖项:
npm install jquery --save
npm install datatables.net --save
npm install datatables.net-dt --save
npm install angular-datatables --save
npm install @types/jquery --save-dev
npm install @types/datatables.net --save-dev
任何想法?
UPDATE:这是我的tsconfig.app.json:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "../out-tsc/app",
"baseUrl": "./",
"module": "es2015",
"allowSyntheticDefaultImports": true
},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
和tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
],
"allowSyntheticDefaultImports": true,
"paths": {
"@angular/*": [
"../node_modules/@angular/*"
]
}
}
}
嗨,你肯定_this正确使用设置好的你这个组件? ..你有没有试过类似的东西:
DataTableDirective.prototype.displayTable =() => {
var _this = this;
this.dtInstance = new Promise(function (resolve, reject) {
Promise.resolve(_this.dtOptions).then(function (dtOptions) {
// Using setTimeout as a "hack" to be "part" of NgZone
setTimeout(function() {
//Error in this line
var dt = $(_this.el.nativeElement).DataTable(dtOptions);
resolve(dt);
});
});
});
};
希望它可以帮助你!
Jquery和jQuery扩展是全局的。你为什么认为这是关闭问题? – omeralper
因为每次我把函数放在我的ts代码中,我就放弃了我的这个范围..所以也许尝试一下胖箭头函数..希望它可以帮助你! –
啊..一定要在angular-cli.json文件中包含你的datatable.js插件..并且要小心翼翼地使用原型 –
检查你的tsconfig.json及其子文件(例如tsconfig.app.json),如果你有空数组array(“types”:[])将其删除。 – omeralper
我有一个,我删除它,但没有改变。 – Burak
您是否已经在angular-cli.json文件中添加了所有文件? – omeralper