[英]how to use SlickGrid (jQuery legacy library) in Angular 4
I'm trying to get a legacy jQuery library called SlickGrid working in Angular 4 and I cannot seem to figure out the best way to do that (I'm also new to Angular 4 so that doesn't help). 我试图在Angular 4中使用一个名为SlickGrid的旧式jQuery库,但我似乎无法找出实现此目的的最佳方法(我对Angular 4还是陌生的 ,所以没有用)。 So far, I found an npm package for @types/slickgrid and installed it.
到目前为止,我为@ types / slickgrid找到了一个npm软件包并安装了它。 I then try to import it in my component with this
然后,我尝试与此一起将其导入我的组件中
import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
import {Grid} from 'slickgrid';
However this throws me some error in the console 但这在控制台中引发了一些错误
app.component.ts(5,24): error TS2306: File 'C:/demo/node_modules/@types/slickgrid/index.d.ts' is not a module.
I also tried with this import 'slickgrid'
but I get the same error 我也尝试使用此
import 'slickgrid'
但出现相同的错误
Here is my full component.ts
file 这是我完整的
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);
});
}
}
And my index.html
where I import the libraries (I could go with WebPack eventually, if I can get this going) 还有我在库中导入的
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>
I really need to get this working and I don't know what else to try. 我真的需要使它正常工作,我不知道还有什么尝试。
Thanks a lot to @Rahul Singh who posted a comment that answers my question. 非常感谢@Rahul Singh发表了评论,回答了我的问题。
Install Angular-CLI and modify the angular-cli.json
file 安装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"
],
}
Do an npm install of the legacy library 进行旧版库的npm安装
npm install slickgrid
add <div>
for the grid in the component.html
file 在
component.html
文件中为网格添加<div>
<div id='myGrid' style='width:600px;height:500px;'></div>
and finally the component.ts
最后是
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);
});
}
}
and it works, again thanks @Rahul 它有效,再次感谢@Rahul
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.