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

时间:2017-09-01 22:05:53

标签: javascript jquery angular typescript slickgrid

我试图在Angular 4中使用名为SlickGrid的遗留jQuery库,我似乎无法找到最好的方法(我也是Angular 4的新手,所以这没有帮助)。到目前为止,我找到了@types/slickgrid的npm包并安装了它。然后我尝试使用此

在我的组件中导入它
import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
import {Grid} from 'slickgrid';

但是这会在控制台中引发一些错误

app.component.ts(5,24): error TS2306: File 'C:/demo/node_modules/@types/slickgrid/index.d.ts' is not a module.

我也试过这个import 'slickgrid',但我得到了同样的错误

这是我的完整component.ts文件

import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
import {Grid} from 'slickgrid';

@Component({
    selector: 'my-app',
    template: `<td valign='top' width='50%'>
        <div id='myGrid' style='width:600px;height:500px;'></div>
    </td>`
})
export class AppComponent implements OnInit {

    ngOnInit(): void {
        let grid;
        let columns = [
        {id: 'title', name: 'Title', field: 'title'},
        {id: 'duration', name: 'Duration', field: 'duration'},
        {id: '%', name: '% Complete', field: 'percentComplete'},
        {id: 'start', name: 'Start', field: 'start'},
        {id: 'finish', name: 'Finish', field: 'finish'},
        {id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven'}
        ];
        let options = {
        enableCellNavigation: true,
        enableColumnReorder: false
        };
        let data = [];
        for (let i = 0; i < 500; i++) {
        data[i] = {
            title: 'Task ' + i,
            duration: '5 days',
            percentComplete: Math.round(Math.random() * 100),
            start: '01/01/2009',
            finish: '01/05/2009',
            effortDriven: (i % 5 === 0)
        };
        }
        $(() => {
        grid = new Grid('#myGrid', data, columns, options);
        });
    }
}

我导入库的index.html(我最终可以使用WebPack,如果可以的话)

    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="styles.css"/>
    <link rel="stylesheet" type="text/css" href="/node_modules/slickgrid-6pac/slick.grid.css"/>
    <link rel="stylesheet" type="text/css" href="/components/css/SlickGrid.css"/>
    </head>
    <body>
        <my-app> Loading... </my-app>
    </body>
    <script src="/node_modules/slickgrid-6pac/lib/jquery-1.11.2.js"></script>
    <script src="/node_modules/slickgrid-6pac/lib/jquery.event.drag-2.2.js"></script>
    <script src="/node_modules/underscore/underscore-min.js"></script>
    <script src="/node_modules/slickgrid-6pac/slick.core.js"></script>
    <script src="/node_modules/slickgrid-6pac/slick.grid.js"></script>
    <!-- Polyfill(s) for older browsers -->
    <script src="/node_modules/core-js/client/shim.min.js"></script>
    <script src="/node_modules/zone.js/dist/zone.min.js"></script>
    <script src="/node_modules/reflect-metadata/Reflect.js"></script>
    <script src="/node_modules/systemjs/dist/system.src.js"></script>
    <script src="/dist/basic_slickgrid_ts/systemjs.config.js"></script>
    <script>
    System.import('app').catch(function(err){ console.error(err); });
    </script>
</html>

我真的需要让这个工作,我不知道还有什么可以尝试。

1 个答案:

答案 0 :(得分:2)

非常感谢@Rahul Singh发表评论回答我的问题。

安装Angular-CLI并修改angular-cli.json文件

{
  "apps": [
  {
    "styles": [
      "../node_modules/slickgrid/slick.grid.css",
      "styles.css"
    ],
    "scripts": [
      "../node_modules/slickgrid/lib/jquery-1.8.3.js",
      "../node_modules/slickgrid/lib/jquery.event.drag-2.2.js",
      "../node_modules/slickgrid/slick.core.js",
      "../node_modules/slickgrid/slick.grid.js"
    ],    
}

执行旧版库的npm安装

npm install slickgrid

<div>文件

中的网格添加component.html
<div id='myGrid' style='width:600px;height:500px;'></div>

最后是component.ts

import { Component, OnInit } from '@angular/core';
import $ from 'jquery/dist/jquery';
import jQuery from 'jquery/dist/jquery';

// using external js modules in Angular
declare var Slick: any;
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'app';

  ngOnInit(): void {
      let grid;
      const columns = [
        {id: 'title', name: 'Title', field: 'title'},
        {id: 'duration', name: 'Duration', field: 'duration'},
        {id: '%', name: '% Complete', field: 'percentComplete'},
        {id: 'start', name: 'Start', field: 'start'},
        {id: 'finish', name: 'Finish', field: 'finish'},
        {id: 'effort-driven', name: 'Effort Driven', field: 'effortDriven'}
      ];
      const options = {
        enableCellNavigation: true,
        enableColumnReorder: false
      };
      let data = [];
      for (let i = 0; i < 500; i++) {
        data[i] = {
          title: 'Task ' + i,
          duration: '5 days',
          percentComplete: Math.round(Math.random() * 100),
          start: '01/01/2009',
          finish: '01/05/2009',
          effortDriven: (i % 5 === 0)
        };
      }
      $(() => {
        grid = new Slick.Grid('#myGrid', data, columns, options);
      });
  }
}

它有效,再次感谢@Rahul