将组件打包到另一个项目时,jQuery Datatable引发错误

时间:2018-07-20 16:04:10

标签: jquery datatables angular5 ng-packagr angular-library

我正在使用Angular 5中的模式库,该库中的组件之一是jquery数据表。模式库是应用程序的一部分,该应用程序包含一些有关如何使用组件的演示页面,并且jquery数据表在该应用程序中可以正常工作。当我将组件打包/导出到另一个应用程序时,导航到使用该组件的页面时出现以下错误:

Error: Uncaught (in promise): TypeError: table.DataTable is not a function

AOT编译器未捕获此错误。

使用this article并进行一些调试,我确定问题是该组件在使用ng-packagr打包时没有正确导入jquery数据表依赖项,即使该依赖项是安装在导入库的应用程序的node_modules中。

这是数据表组件:

import { HttpClient } from '@angular/common/http';
import { Component, OnInit, ChangeDetectorRef, Input, Output, EventEmitter, ContentChild, TemplateRef } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';

import * as jquery from 'jquery';
const $: JQueryStatic = (<any>jquery).default || jquery;
import 'datatables.net';
import 'datatables.net-bs4';
import 'datatables.net-select';
import 'datatables.net-buttons';
import 'datatables.net-buttons/js/buttons.colVis.js';
import 'datatables.net-select-dt';
import { PARAMETERS } from '@angular/core/src/util/decorators';


@Component({
  selector: 'lib-datatable',
  templateUrl: './datatable.component.html'
})
export class DataTableComponent implements OnInit {
   data: any[];
   dataTable: any;
   allSelected = false;

...
...
...

   constructor(private http: HttpClient, private chRef: ChangeDetectorRef) {}

   ngOnInit() {
        Function.call(this.dataFunction(this.dataParams).then((data: any[]) => {
            this.data = data;
            const table: any = $('#' + this.tableId);

            this.prepareBulkActionBtns();
            // this.tableSettings.buttons.push(this.manageColsBtn);
            this.prepareColDefs();
            this.tableSettings.data = data;
            this.dataTable = table.DataTable(this.tableSettings);

            this.dataTable.on( 'select deselect', function ( e, dt, type, indexes ) {
                const selectedRows = dt.rows( { selected: true } ).count();
                dt.button('.bulkAction').enable( selectedRows > 0 );
            } );
            $('.ellipsis-menu').off('click').on('click', function(event) {

                if ($('[aria-labelledby="' + $(this).data('id') + '"]').css('display') === 'none') {
                    $('.row-menu').css('display', 'none');
                    $('[aria-labelledby="' + $(this).data('id') + '"]').css('display', 'block');
                } else {
                    $('[aria-labelledby="' + $(this).data('id') + '"]').css('display', 'none');
                }
            });
            const component = this;
            $('.row-action').off('click').on('click', function() {
                const id = $(this).data('id');
                const action = $(this).data('action');
                $('.row-menu').css('display', 'none');
                component.selectRowAction(action, id);
            });

        }));
    }
...
...
...

这是库的package.json

{
  "name": "components-patternlib",
  "version": "0.0.27",
  "license": "MIT",
  "$schema": "./node_modules/ng-packagr/package.schema.json",
  "ng-package": {
    "lib": {
      "entryFile": "public_api.ts"
    }
  },
  "scripts": {
    "ng": "ng",
    "start": "ng serve --aot --port 4200 --host 0.0.0.0",
    "buildscss": "scss-bundle -e ./src/sass/styles.scss -d ./dist/styles.scss",
    "build": "ng-packagr -p package.json && npm run buildscss",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "ngPackage": {
    "lib": {
      "entryFile": "./src/app/mainapp/public_api.ts",
      "externals": {
        "pako/dist/pako_deflate": "pako"
      }
    }
  },
  "publishConfig": {
    "registry": "http://someregistry.com",
    "tag": "latest"
  },
  "peerDependencies": {
    "@angular/animations": "^5.2.9",
    "@angular/cdk": "^5.2.5",
    "@angular/common": "^5.2.9",
    "@angular/compiler": "^5.2.9",
    "@angular/core": "^5.2.9",
    "@angular/forms": "^5.2.9",
    "@angular/http": "^5.2.9",
    "@angular/platform-browser": "^5.2.9",
    "@angular/platform-browser-dynamic": "^5.2.9",
    "@angular/router": "^5.2.9",
    "@ng-bootstrap/ng-bootstrap": "^1.0.2",
    "@ngx-translate/core": "^9.1.1",
    "@ngx-translate/http-loader": "^2.0.1",
    "@progress/kendo-angular-buttons": "^3.0.4",
    "@progress/kendo-angular-charts": "^3.2.1",
    "@progress/kendo-angular-dateinputs": "^1.4.5",
    "@progress/kendo-angular-dialog": "^1.3.0",
    "@progress/kendo-angular-dropdowns": "^1.6.1",
    "@progress/kendo-angular-excel-export": "^1.0.7",
    "@progress/kendo-angular-grid": "^1.8.0",
    "@progress/kendo-angular-inputs": "^1.5.0",
    "@progress/kendo-angular-intl": "^1.4.1",
    "@progress/kendo-angular-l10n": "^1.2.0",
    "@progress/kendo-angular-layout": "^1.2.1",
    "@progress/kendo-angular-popup": "^1.3.4",
    "@progress/kendo-angular-resize-sensor": "^1.0.5",
    "@progress/kendo-angular-upload": "^4.1.2",
    "@progress/kendo-charts": "^1.9.0",
    "@progress/kendo-data-query": "^1.2.1",
    "@progress/kendo-drawing": "^1.5.6",
    "@progress/kendo-theme-bootstrap": "^2.13.6",
    "@telerik/kendo-dropdowns-common": "^1.0.0",
    "@types/jquery": "^3.3.4",
    "angular-aria": "^1.7.2",
    "bootstrap": "^4.0.0",
    "core-js": "^2.5.7",
    "datatables.net": "^1.10.19",
    "datatables.net-bs4": "^1.10.19",
    "datatables.net-buttons-dt": "^1.5.3",
    "datatables.net-dt": "^1.10.19",
    "datatables.net-responsive-dt": "^2.2.3",
    "datatables.net-select-dt": "^1.2.7",
    "font-awesome": "^4.7.0",
    "jquery": "^3.3.1",
    "messageformat": "^1.0.0",
    "ng-click-outside": "^3.3.0",
    "ngx-scroll-event": "^1.0.8",
    "ngx-translate-messageformat-compiler": "^2.0.1",
    "popper.js": "^1.14.1",
    "prismjs": "^1.15.0",
    "rxjs": "^5.5.11",
    "zone.js": "^0.8.19"
  },
  "dependencies": {
    "@angular/animations": "^5.2.9",
    "@angular/cdk": "^5.2.5",
    "@angular/common": "^5.2.9",
    "@angular/compiler": "^5.2.9",
    "@angular/core": "^5.2.9",
    "@angular/forms": "^5.2.9",
    "@angular/http": "^5.2.9",
    "@angular/platform-browser": "^5.2.9",
    "@angular/platform-browser-dynamic": "^5.2.9",
    "@angular/router": "^5.2.9",
    "@ng-bootstrap/ng-bootstrap": "^1.0.2",
    "@ngx-translate/core": "^9.1.1",
    "@ngx-translate/http-loader": "^2.0.1",
    "@progress/kendo-angular-buttons": "^3.0.4",
    "@progress/kendo-angular-charts": "^3.2.1",
    "@progress/kendo-angular-dateinputs": "^1.4.5",
    "@progress/kendo-angular-dialog": "^1.3.0",
    "@progress/kendo-angular-dropdowns": "^1.6.1",
    "@progress/kendo-angular-excel-export": "^1.0.7",
    "@progress/kendo-angular-grid": "^1.8.0",
    "@progress/kendo-angular-inputs": "^1.5.0",
    "@progress/kendo-angular-intl": "^1.4.1",
    "@progress/kendo-angular-l10n": "^1.2.0",
    "@progress/kendo-angular-layout": "^1.2.1",
    "@progress/kendo-angular-popup": "^1.3.4",
    "@progress/kendo-angular-resize-sensor": "^1.0.5",
    "@progress/kendo-angular-upload": "^4.1.2",
    "@progress/kendo-charts": "^1.9.0",
    "@progress/kendo-data-query": "^1.2.1",
    "@progress/kendo-drawing": "^1.5.6",
    "@progress/kendo-theme-bootstrap": "^2.13.6",
    "@telerik/kendo-dropdowns-common": "^1.0.0",
    "@types/jquery": "^3.3.4",
    "angular-aria": "^1.7.2",
    "bootstrap": "^4.0.0",
    "core-js": "^2.5.7",
    "datatables.net": "^1.10.19",
    "datatables.net-bs4": "^1.10.19",
    "datatables.net-buttons-dt": "^1.5.3",
    "datatables.net-dt": "^1.10.19",
    "datatables.net-responsive-dt": "^2.2.3",
    "datatables.net-select-dt": "^1.2.7",
    "font-awesome": "^4.7.0",
    "jquery": "^3.3.1",
    "messageformat": "^1.0.0",
    "ng-click-outside": "^3.3.0",
    "ngx-scroll-event": "^1.0.8",
    "ngx-translate-messageformat-compiler": "^2.0.1",
    "popper.js": "^1.14.1",
    "prismjs": "^1.15.0",
    "rxjs": "^5.5.11",
    "zone.js": "^0.8.19"
  },
  "devDependencies": {
    "@angular/cli": "^1.7.3",
    "@angular/compiler-cli": "^5.2.9",
    "@angular/language-service": "^5.2.9",
    "@types/jasmine": "^2.8.8",
    "@types/jasminewd2": "^2.0.3",
    "@types/node": "^8.10.20",
    "angular-in-memory-web-api": "^0.5.4",
    "codelyzer": "^4.2.1",
    "jasmine-core": "^2.99.1",
    "jasmine-spec-reporter": "^4.2.1",
    "karma": "^2.0.4",
    "karma-chrome-launcher": "^2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.4.2",
    "karma-jasmine": "^1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "ng-packagr": "1.6.0",
    "protractor": "^5.3.0",
    "puppeteer": "^0.13.0",
    "scss-bundle": "^2.3.2",
    "ts-node": "^4.0.2",
    "tslint": "^5.9.1",
    "typescript": "~2.4.2",
    "uglify-js": "^3.3.18"
  }
}

ng-package.json除了它仅具有peerDependencies而没有依赖项或devDependencies之外,是相同的。

这是导入库的项目的package.json:

{
  "name": "prototype",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^5.2.11",
    "@angular/cdk": "^5.2.5",
    "@angular/common": "^5.2.11",
    "@angular/compiler": "^5.2.11",
    "@angular/core": "^5.2.11",
    "@angular/forms": "^5.2.11",
    "@angular/http": "^5.2.11",
    "@angular/platform-browser": "^5.2.11",
    "@angular/platform-browser-dynamic": "^5.2.11",
    "@angular/router": "^5.2.11",
    "@ng-bootstrap/ng-bootstrap": "^1.0.2",
    "@ngx-translate/core": "^9.1.1",
    "@ngx-translate/http-loader": "^2.0.1",
    "@progress/kendo-angular-buttons": "^3.0.4",
    "@progress/kendo-angular-charts": "^3.2.1",
    "@progress/kendo-angular-dateinputs": "^1.4.5",
    "@progress/kendo-angular-dialog": "^1.3.0",
    "@progress/kendo-angular-dropdowns": "^1.6.1",
    "@progress/kendo-angular-excel-export": "^1.0.7",
    "@progress/kendo-angular-grid": "^1.8.0",
    "@progress/kendo-angular-inputs": "^1.5.0",
    "@progress/kendo-angular-intl": "^1.3.2",
    "@progress/kendo-angular-l10n": "^1.2.0",
    "@progress/kendo-angular-layout": "^1.2.1",
    "@progress/kendo-angular-popup": "^1.3.4",
    "@progress/kendo-angular-resize-sensor": "^1.0.5",
    "@progress/kendo-angular-upload": "^4.1.2",
    "@progress/kendo-data-query": "^1.2.1",
    "@progress/kendo-drawing": "^1.5.1",
    "@progress/kendo-theme-bootstrap": "^2.11.9",
    "@telerik/kendo-dropdowns-common": "^1.0.0",
    "@types/jquery": "^3.3.4",
    "angular-aria": "^1.6.9",
    "bootstrap": "^4.0.0",
    "components-patternlib": "latest",
    "core-js": "^2.5.3",
    "datatables.net": "^1.10.19",
    "datatables.net-bs4": "^1.10.19",
    "datatables.net-buttons-dt": "^1.5.3",
    "datatables.net-dt": "^1.10.19",
    "datatables.net-responsive-dt": "^2.2.3",
    "datatables.net-select-dt": "^1.2.7",
    "font-awesome": "^4.7.0",
    "jquery": "^3.3.1",
    "messageformat": "^1.0.0",
    "ng-click-outside": "^3.3.0",
    "ngx-scroll-event": "^1.0.8",
    "ngx-translate-messageformat-compiler": "^2.0.1",
    "popper.js": "^1.14.1",
    "prismjs": "^1.14.0",
    "rxjs": "^5.5.11",
    "uuid": "^3.3.0",
    "zone.js": "^0.8.19"
  },
  "devDependencies": {
    "@angular/cli": "^1.7.3",
    "@angular/compiler-cli": "^5.2.11",
    "@angular/language-service": "^5.2.11",
    "@types/jasmine": "^2.8.6",
    "@types/jasminewd2": "^2.0.3",
    "@types/node": "^8.9.5",
    "angular-in-memory-web-api": "^0.5.4",
    "codelyzer": "^4.2.1",
    "jasmine-core": "^2.99.1",
    "jasmine-spec-reporter": "^4.2.1",
    "karma": "^2.0.0",
    "karma-chrome-launcher": "^2.2.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.4.2",
    "karma-jasmine": "^1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "^5.3.0",
    "puppeteer": "^0.13.0",
    "ts-node": "^4.0.2",
    "tslint": "^5.9.1",
    "typescript": "~2.4.2"
  }
}

为什么组件无法在原型应用程序中找到数据表依赖关系,但可以找到jquery依赖关系?是否可以声明多个jquery实例?我不这么认为,但也许其他人可以确定是否是这种情况。

如何获取数据表组件以始终能够找到jquery数据表依赖关系,无论它是在模式库中还是在导入模式库组件的原型中?

0 个答案:

没有答案