首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在为Angular 4使用Angular数据表时,DataTable不是函数TypeError

在为Angular 4使用Angular数据表时,DataTable不是函数TypeError
EN

Stack Overflow用户
提问于 2017-10-20 18:38:19
回答 3查看 2.8K关注 0票数 1

在我的代码中使用angular-datatables时,我得到了TypeError。

代码语言:javascript
运行
复制
ERROR TypeError: $(...).DataTable is not a function
at angular-datatables.directive.js:38  

指令中的代码是:

代码语言:javascript
运行
复制
 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);
            });
        });
    });
};

这里的问题是什么?我通过以下方式安装库和依赖项:

代码语言:javascript
运行
复制
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

有什么想法吗?

更新:这是我的tsconfig.app.json:

代码语言:javascript
运行
复制
{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "baseUrl": "./",
    "module": "es2015",
    "allowSyntheticDefaultImports": true
 },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ]

}

和tsconfig.json:

代码语言:javascript
运行
复制
{
  "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/*"
      ]
    }
  }
}
EN

回答 3

Stack Overflow用户

发布于 2017-10-20 18:45:04

您好,您确定组件的_this设置正确吗?你有没有尝试过这样的东西:

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2018-07-15 21:05:26

代码语言:javascript
运行
复制
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

然后

在angular.json中添加

代码语言:javascript
运行
复制
enter code here
 {
 "projects": {
    "your-app-name": {
    "architect": {
    "build": {
      "options": {
        "styles": [
          "node_modules/datatables.net-dt/css/jquery.dataTables.css"========>*
        ],
        "scripts": [
          "node_modules/jquery/dist/jquery.js",==============================>*
          "node_modules/datatables.net/js/jquery.dataTables.js"==============>*
        ],

使用cmd重启

票数 0
EN

Stack Overflow用户

发布于 2020-07-14 15:56:45

我把它们都放在angular.json中解决了这个问题

代码语言:javascript
运行
复制
"scripts": [
    "node_modules/jquery/dist/jquery.min.js", 
    "node_modules/datatables.net/js/jquery.dataTables.js"
]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46847380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档