如何在angular 2中使用Handsontable库

希里什(Shirish)

我是npm中配置部分的新手,我试图在使用angular-cli(ng init)创建的angular 2项目中使用handontable库。我为它添加了打字稿定义。

这是我的app.compponent.ts

import { Component } from '@angular/core';
import 'handsontable/dist/handsontable.full.js';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';
  ngOnInit() {
  }
  ngAfterViewInit() {
    const myData = [
            ["1", "2", "3", "4", "5", "6"]
        ];

        const config = {
            data: myData,
            colHeaders: ['A', 'B', 'C', 'D', 'E', 'F'],
            startRows: 5,
            startCols: 5,
            minSpareCols: 1,
            //always keep at least 1 spare row at the right
            minSpareRows: 1
            //always keep at least 1 spare row at the bottom,
        };
        (<any>$("#test")).handsontable(config);
  }
}

app.component.html

<h1>
  <div id ="test"></div>
</h1>

index.html

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>AngularStart2</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root>Loading...</app-root>
</body>
</html>

tsconfig.js

{
  "compilerOptions": {
    "baseUrl": "",
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"],
    "mapRoot": "./",
    "module": "es6",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types"
    ]
  }
}

给我错误:

ReferenceError: Handsontable is not defined at r.fn.init.$.fn.handsontable (http://localhost:4200/vendor.bundle.js:87583:26) at AppComponent.ngAfterViewInit (http://localhost:4200/main.bundle.js:79:20)

我也试图以下面的方式导入

import {Handsontable} from 'handsontable';

它给了我以下错误

Uncaught Error: Cannot find module 'numbro'
    at newRequire (handsontable.js:53) [<root>]
    at :8000/vendor.bundle.js:102461:16 [<root>]
    at Object.23.cellTypes (handsontable.js:4439) [<root>]
    at newRequire (handsontable.js:58) [<root>]

我已经尝试过https://github.com/handsontable/handsontable/issues/3627的所有方法

桑凯特

目前,HandsOnTable正式不支持Angular2。请在此处参考论坛参考计划于今年第一季度。您可以在此处对该功能进行投票

因此,您将必须像这样直接将handsontable js&css文件用于angular2应用程序中,

步骤1:在Index.html中,添加以下两行:

<link rel="stylesheet" href="http://docs.handsontable.com/0.30.1/bower_components/handsontable/dist/handsontable.full.css">

<script src="http://docs.handsontable.com/0.30.1/bower_components/handsontable/dist/handsontable.full.js"></script>

第2步:示例AppComponent.ts看起来像这样-

import { Component, AfterViewInit } from '@angular/core';

declare var Handsontable: any;

@Component({
    selector: 'my-app',
    template: `
<h3>Angular2 Final 2.0.0</h3>
<h2>Handsontable Performance Test (10000x100)</h2>

<div id="example"></div>
    `
})

export class AppComponent implements AfterViewInit { 

container: any;
hot: any;

ngAfterViewInit() {
this.container = document.getElementById('example');

this.hot = new Handsontable(this.container, {
  data: Handsontable.helper.createSpreadsheetData(10000, 100),
  rowHeaders: true,
  colHeaders: true,
  // performance tip: set constant size
  colWidths: 80,
  rowHeights: 23,
  // performance tip: turn off calculations
  autoRowSize: false,
  autoColSize: false,
});

}

}

结果输出

结果

请注意-我正在使用angular2最终版本2.0.0

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在Angular 2中使用子路线

来自分类Dev

如何在Angular2中使用<html>模板?

来自分类Dev

如何在Angular2中使组件通用

来自分类Dev

如何在Angular 2中使用SignalR?

来自分类Dev

在Angular 2中使用Angular 1库吗?

来自分类Dev

如何在Angular 2中使用jQuery?

来自分类Dev

如何在Angular 2中使用Less?

来自分类Dev

如何在Angular2中使ngOnChanges工作

来自分类Dev

如何在angular2中使用异步主题?

来自分类Dev

如何在Angular 2中使用FullCalendar

来自分类Dev

如何在Angular 2中包含外部JavaScript库?

来自分类Dev

如何在ionic 2中使用第三方javascript库(pixijs)?

来自分类Dev

如何在angular 2中使用underscore.js库

来自分类Dev

如何在Angular 2中使用canReuse和routerOnReuse

来自分类Dev

如何在Angular2中使用MockBackend

来自分类Dev

如何在Retrofit 2中使用GitHub API创建存储库?

来自分类Dev

如何在Angular2中使用<html>模板?

来自分类Dev

如何在Django 1.8中使用Angular 2?

来自分类Dev

如何在Angular2中使用bootstrap / jquery

来自分类Dev

如何在angular2中使用异步主题?

来自分类Dev

如何在Angular 2中使用canReuse和routerOnReuse

来自分类Dev

如何在Angular 2中使用@CanActivate

来自分类Dev

如何在Angular 2中使用Kendo UI Slider

来自分类Dev

如何在Angular 2中使用Bootstrap CSS库?

来自分类Dev

如何在Magento 2中使用Zend_Barcode库?

来自分类Dev

如何在Angular 2中使用窗口对象?

来自分类Dev

我们如何在 angular2 或 4 中使用 bootstrap-sweetalert 库?

来自分类Dev

如何在 Angular2 中使用 Owl Carousel 2?

来自分类Dev

如何在 Angular 4 中使用 SlickGrid(jQuery 遗留库)