2017-10-20 3 views
0

Ich bekomme TypeError bei der Verwendung von Angular-Datatables in meinem Code.DataTable ist keine Funktion TypeError bei der Verwendung von Angular-Datatables für Angular 4

ERROR TypeError: $(...).DataTable is not a function 
at angular-datatables.directive.js:38 

-Code in der Richtlinie ist:

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); 
      }); 
     }); 
    }); 
}; 

Was ist das Problem hier? Ich installierte die Bibliothek und Abhängigkeiten von:

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 

Eine Idee?

UPDATE: Hier ist mein tsconfig.app.json:

{ 
    "extends": "../tsconfig.json", 
    "compilerOptions": { 
    "outDir": "../out-tsc/app", 
    "baseUrl": "./", 
    "module": "es2015", 
    "allowSyntheticDefaultImports": true 
}, 
    "exclude": [ 
    "test.ts", 
    "**/*.spec.ts" 
    ] 

}

und 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

Überprüfen Sie Ihre tsconfig.json und seine untergeordneten Dateien (z. B. tsconfig.app.json), wenn Sie leere Typen Array ("types": []) entfernen Sie es. – omeralper

+0

Ich hatte einen und ich entfernte ihn, aber keine Änderung. – Burak

+0

Haben Sie alle Dateien in der angular-cli.json-Datei hinzugefügt? – omeralper

Antwort

0

Hallo sind Sie sicher, dass die _this richtig mit Ihrem setted ist das der Komponente? .. haben Sie versucht, etwas wie:

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); 
      }); 
     }); 
    }); 
}; 

Ich hoffe, es hilft Ihnen!

+0

Jquery und jquery Erweiterungen sind global. Warum denken Sie, es handelt sich um ein Schließungsproblem? – omeralper

+0

Ursache Jedes Mal, wenn ich Funktion in meinem ts Code setze, verliere ich mein diesen Bereich .. so vielleicht einen Versuch, die fette Pfeilfunktion .. hoffe, es hilft Ihnen! –

+0

ah ..be sicher, dass Sie Ihre datatable.js-Plugin in angular-cli.json-Datei enthalten .. und vorsichtig sein, Prototyp zu verwenden –

Verwandte Themen