前端框架一直這最近幾年特別火的一個話題,尤爲是Angular 2擁有衆多的粉絲。在2016年9月份Angular 2正式發佈以後,大量的粉絲的開始投入到了Angular 2的懷抱。固然這其中也包括我。若是你想了解Angular 2,推薦官方網站:英文版、中文版。經過快速起步,能夠快速體驗Angular 2。 公司的一個項目想基於Angular 2的2.4 版本進行開發,目前還在進行前期的調研階段。我擔當的任務就是研究基於Angular 2的UI控件,在官方網站的資源中列出了不少支持Angular 2的資源。發現Wijmo的Flexgrid控件已經支持Angular 2的2.4版本,初步知足咱們的需求。css
1、環境搭建 Angular 2不只是功能上和Angular 1有不少的差異,環境搭建也是區別很大。不少初學者反饋Angular 2的代碼很難運行起來。Angular2是基於ES6來開發的,因此會有不少第三方依賴。因爲不少瀏覽器還不支持ES6,因此Angular2引入了不少polyfill或者shim, 致使咱們引入了第三方依賴。下面以FlexGrid爲例來講明如何搭建運行環境。 一、 安裝NodeJS 能夠從Node官網下載 https://nodejs.org/en/download/。 二、 新建目錄來存放項目 mkdir ng2-flexGrid cd ng2-flexGrid 三、 配置文件 package.json 用來標記項目須要使用的npm依賴包。 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 { "name": "wj-ng2-flexgrid", "version": "1.0.0", "scripts": { "start": "tsc && concurrently "tsc -w" "lite-server" ", "lite": "lite-server", "tsc": "tsc", "tsc:w": "tsc -w" }, "licenses": [ { "type": "MIT", "url": "https://github.com/angular/angular.io/blob/master/LICENSE" } ], "dependencies": { "@angular/common": "~2.1.1", "@angular/compiler": "~2.1.1", "@angular/core": "~2.1.1", "@angular/forms": "~2.1.1", "@angular/http": "~2.1.1", "@angular/platform-browser": "~2.1.1", "@angular/platform-browser-dynamic": "~2.1.1", "@angular/router": "~3.1.1", "@angular/upgrade": "~2.1.1", "angular-in-memory-web-api": "~0.1.13", "core-js": "^2.4.1", "reflect-metadata": "^0.1.8", "rxjs": "5.0.0-beta.12", "systemjs": "0.19.39", "zone.js": "^0.6.25" }, "devDependencies": { "@types/core-js": "^0.9.34", "@types/node": "^6.0.45", "concurrently": "^3.0.0", "lite-server": "^2.2.2", "typescript": "^2.0.3" } } tsconfig.json TypeScript的配置文件,定義TypeScript 編譯器如何從項目源文件生成 JavaScript 代碼。 ? 1 2 3 4 5 6 7 8 9 10 11 12 { "compilerOptions": { "target": "es5", "module": "commonjs", "moduleResolution": "node", "sourceMap": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "removeComments": false, "noImplicitAny": false } } systemjs.config.js 爲SystemJS(模塊加載器)提供到哪裏查找應用模塊的信息,並註冊了全部必備的依賴包。 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 /**html
System configuration for Angular samples
Adjust as necessary for your application needs. */ (function (global) { System.config({ paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { // our app is within the app folder app: 'app', // angular bundles '@angular/core': 'npm:@angular/core/bundles/core.umd.js', '@angular/common': 'npm:@angular/common/bundles/common.umd.js', '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js', '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js', '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js', '@angular/http': 'npm:@angular/http/bundles/http.umd.js', '@angular/router': 'npm:@angular/router/bundles/router.umd.js', '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js', '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js', // other libraries 'rxjs': 'npm:rxjs', 'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { app: { main: './main.js', defaultExtension: 'js' }, rxjs: { defaultExtension: 'js' } } }); })(this); 四、 運行npm install
NPM會根據package.json中定義的包進行安裝。會產生一個node_modules目錄,將這些包放在這裏。 至此環境搭建的任務就已經完成了。下面咱們以FlexGrid爲例說明支持Angular 2。 2、支持Angular 2的表格控件如何使用 一、HTML ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
使用 Angular 2 來建立FlexGrid控件
Loading
在HTML宿主頁面中,除了Angular 2中必須的組件,還須要引入Wijmo腳本。
二、編寫數據服務
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
'use strict'
import { Injectable } from '@angular/core';
@Injectable()
export class DataService {
getData(count: number): wijmo.collections.ObservableArray {
var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(','),
data = new wijmo.collections.ObservableArray();
for (var i = 0; i < count; i++) {
data.push({
id: i,
country: countries[i % countries.length],
date: new Date(2014, i % 12, i % 28),
amount: Math.random() * 10000,
active: i % 4 == 0
});
}
return data;
}
}
三、編寫根組件
如今咱們編寫應用的第一個組件:根組件 app.component ,也是這個程序惟一的組件。在這個組件中,須要引入兩個元標記:Component, Inject。還須要注入定義的數據服務data.Service。
app.component.ts:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { Component, Inject } from '@angular/core';
import { DataService } from '../services/data.service';
@Component ({
selector:'app-cmp',
templateUrl:'app/components/app.component.html',
})
export class AppComponent{
protected dataSvc:DataService;
data: wijmo.collections.CollectionView;
constructor(@Inject(DataService) dataSvc:DataService){
this.dataSvc = dataSvc;
this.data = new wijmo.collections.CollectionView(this.dataSvc.getData(50));
}
}
app.component.html:
?
1
2
3
4
5
6
7
8
前端
<wj-flex-grid [itemsSource]="data">
在這裏僅僅須要引入wj-flex-grid標記,就能夠建立FlexGrid控件。wj-flex-grid 組件是做爲一個子組件存在,在app.module 模塊中注入。itemsSource 綁定一個數據源,這個itemsSource是flexgrid已經封裝完成的屬性。
在Angular 2下使用FlexGrid的最大好處就是:Angular 2組件提供了使用標記語言來聲明控件的能力。聲明標記很好地遵循了MVVM設計模式,咱們能夠徹底經過View(標記語言)來配置咱們的組件。FlexGrid支持使用Angular 2標記語言來聲明完整的API。你徹底可使用標記語言設置屬性,附加事件,配置子組件。
四、編寫根模塊
在根模塊中將組件注入,須要將引用的全部的組件和模塊都要注入進來。
?
1
2
3
4
5
6
7
8
9
10
11
12
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { WjGridModule } from 'wijmo/wijmo.angular2.grid';
import { AppComponent } from './components/app.component';
import { DataService } from './services/data.service';
@NgModule({
imports: [ WjGridModule, BrowserModule],
declarations: [AppComponent],
providers:[DataService],
bootstrap: [AppComponent],
})
export class AppModule { }
五、引導程序
main.ts:
?
1
2
3
4
5
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import {enableProdMode} from '@angular/core';
import { AppModule } from './app.module';
enableProdMode();
platformBrowserDynamic().bootstrapModule(AppModule);
3、運行
在命令行執行 npm start,這時,程序會自動打開默認瀏覽器並渲染頁面。
start 命令是執行定義在 package.json 文件中的scripts命令。 會將ts代碼編譯爲原生js,而且會啓動一個靜態服務器。 這個服務器會檢測文件的變化,當發現文件改動,那麼會自動編譯ts代碼。
下面是運行的結果:
FlexGrid內置的基本功能好比:排序、過濾、分組、編輯等,也以經過可選的擴展來提供其餘功能。FlexGrid和其它產品比較,性能仍是不錯的。它的文件尺寸比較小壓縮後約25K。 下載源代碼:http://xiazai.jb51.net/201701/yuanma/ng2-flexGrid_jb51.rarnode