2017-10-20 68 views
0

我在代碼中使用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/*" 
     ] 
    } 
    } 
} 
+0

檢查你的tsconfig.json及其子文件(例如tsconfig.app.json),如果你有空數組array(「types」:[])將其刪除。 – omeralper

+0

我有一個,我刪除它,但沒有改變。 – Burak

+0

您是否已經在angular-cli.json文件中添加了所有文件? – omeralper

回答

0

嗨,你肯定_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); 
      }); 
     }); 
    }); 
}; 

希望它可以幫助你!

+0

Jquery和jQuery擴展是全局的。你爲什麼認爲這是關閉問題? – omeralper

+0

因爲每次我把函數放在我的ts代碼中,我就放棄了我的這個範圍..所以也許嘗試一下胖箭頭函數..希望它可以幫助你! –

+0

啊..一定要在angular-cli.json文件中包含你的datatable.js插件..並且要小心翼翼地使用原型 –

相關問題