Angular DataTables:$(...).DataTable 不是函数

Angular DataTables: $(...).DataTable is not a function

Angular 版本:6.0.4 ~ 节点版本:10.4.1 ~ NPM 版本:6.1.0

我看到这个问题被问过很多次,但没有人回答。

在遵循这些 instructions to install angular-datables, and trying to use the directive on a table, as in their Zero Configuration example 之后,我不断收到此错误:

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

包含的样式和脚本

"styles": [
   "node_modules/bootstrap/dist/css/bootstrap.min.css",
   "node_modules/datatables.net-dt/css/jquery.dataTables.css",
   "src/styles.css"
],
"scripts": [
   "node_modules/jquery/dist/jquery.min.js",
   "node_modules/bootstrap/dist/js/bootstrap.min.js",
   "node_modules/datatables.net/js/jquery.dataTables.js"
]

导入 app.module.ts

import { DataTablesModule } from 'angular-datatables';

DataTablesModule 已添加到导入数组。

*.component.html

<h1>View Accounts</h1>
<table class='table table-dark text-center table-striped table-hover rounded' datatable>
  <thead class='thead-dark'>
    <tr>
      <td>#</td>
      <td>Account Name</td>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor='let account of db.accounts; let i = index' routerLink='/account/{{account.id}}'>
      <td>{{i+1}}</td>
      <td>{{account.accountHolder}}</td>
    </tr>
  </tbody>
</table>

通过刷新我的节点模块修复了错误

rm -rf node_modules/
npm cache clear
npm install

我可能安装了两个版本的 jQuery,在数据表绑定到它之后重置了我的 jQuery 实例。

    Angular version(9 and above):
    
    component.ts
    ------------
    import * as $ from 'jquery';
    import 'datatables.net';
    import { from, Subject } from 'rxjs';
    
    export class testComponent implements OnInit {
      dtOptions: DataTables.Settings = {};
      dtTrigger: Subject<any> = new Subject();
      
    }

component.html
--------------
 
 <table  [dtTrigger]="dtTrigger" id="example" datatable  class="row-border hover">
     <thead><tr><th></th></tr></thead>
    <tbody>....</tbody>
<table>

我错过了'“node_modules/datatables.net/js/jquery.dataTables.js”, ' 在 angular.json 它应该看起来像

"scripts": [
          "node_modules/jquery/dist/jquery.min.js",
          "node_modules/datatables.net/js/jquery.dataTables.js",
          "node_modules/bootstrap/dist/js/bootstrap.js"
        ]