未加星标

Setting up a boilerplate for Angular 2

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
Introduction

I guess we all say amen to the following quote:

“The Only Thing That Is Constant Is Change” Heraclitus

Working with XPages / IBM Notes you get scare-mongered a couple times a year thatthe whole thing is dead. Withthe Java vs javascript deathmatchnot decidedyet as ICS developer you might wonder on what train to jump next.

In the past I have been looking at Angular but never took it to the next level. With Angular 2 recently official released the cards might be different this time. Just look at the job vacancy sites and Angular specialists are searched aftereverywhere.

So I took a course Angular 2 and now I am migrating an XPages app to Angular 2 to see if the love is both directions.

Angular 2 boilerplate

A big difference with the previous version of Angular is the preparation. Angular 2 requires some sort of boilerplate, which I will describe how to build in this post.

WebContent/ODP

I will be using Domino as the HTTP server, since my data still resides in NSF. So for now my Angular app will live in the WebContent folder, so that is the working directoryof my application. For ease of working I have setup a Github project. Added an ODP folder and synched my NSF with that.

In the WebContent folder in my ODP I will create the following documents:

tsconfig.json typings.json package.json app/app.component.ts app/main.ts index.html

If you want you can just download a quickstart somewhere.

tsconfig.json

Create this file in theroot of yourproject. In my course Typescript is used, but you could also script in Angular withJavaScript of course.

tsconfig definesthe compiler configuration, here we say thatour target code is JavaScript.

{

“compilerOptions”: {

“target” : “es5”,

“module” : “system”,

“moduleResolution” : “node”,

“sourceMap” : true,

“emitDecoratorMetadata” : true,

“experimentalDecorators”: true,

“removeComments” : false,

“noImplicitAny” : false

},

“exclude” : [

“node_modules”,

“typings/main”,

“typings/main.d.ts”

]

}

typings.json

Also place this file in the root of your project. In this file you define the Typescript declarations. It turns out that the Typescript compiler does not recognize severallibraries…

{

“ambientDependencies”: {

“es6-shim”: “github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd”,

“jasmine” : “github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd”

}

}

package.json

This file will also be stored in the root of the project. In this file all dependencies and it’s versions are registered, which we will install with NPM.

{

“name” : “angular2-helloWorld”,

“version” : “1.0.0”,

“scripts” : {

“start” : “concurrently \”npm run tsc:w\” \”npm run lite\” “,

“tsc” : “tsc”,

“tsc:w” : “tsc -w”,

“lite” : “lite-server”,

“typings” : “typings”,

“postinstall”: “typings install”

},

“license” : “ISC”,

“dependencies” : {

“angular2” : “latest”,

“bootstrap” : “^3.3.6”,

“systemjs” : “0.19.24”,

“es6-shim” : “^0.35.0”,

“reflect-metadata”: “0.1.2”,

“rxjs” : “5.0.0-beta.2”,

“zone.js” : “0.6.6”

},

“devDependencies”: {

“concurrently”: “^2.0.0”,

“lite-server” : “^2.1.0”,

“typescript” : “^1.8.9”,

“typings” : “^0.7.9”

},

“author” : “Patrick Kwinten << https://se.linkedin.com/in/patrickkwinten>>&#8221 ;

}

app.component.ts

An Angular 2 app needs at minimum 1 component, the root component. Components are the basic building blocks of Angular applications. A component controls a portion of the screen―a view―through its associated template.

import {Component} from ‘angular2/core’;

@Component({

selector: ‘hello-world’,

template: ‘<h1>Hello World</h1>’

})

export class AppComponent {

// …here we will add our logic later

}

So first you define a component, and then you make it available for export soyou can import it into the application.

main.ts

This file is the bootstrapper in our app. Since our boilerplate is simple it does not contain so much yet:

import {bootstrap} from ‘angular2/platform/browser’

import {AppComponent} from ‘./app.component’

bootstrap(AppComponent);

Note: At this moment we do not have declared to use any module so we do not have to create anapp/app.module.ts file.

index.html

The index file contains out of 2 sections, the first is the header, the other one the body. The header can be divided into 3 steps:

add styling load the libraries configure our main.js

<html>

<head>

<meta charset=”UTF-8″>

<title>Angular 2 Hello World</title>

<meta name=”viewport” content=”width=device-width, initial-scale=1″>

<! Step 1 >

<link rel=”stylesheet” href=”node_modules/bootstrap/dist/css/bootstrap.min.css”>

<! Step 2 >

<! IE required polyfills, in this exact order >

http://node_modules/es6-shim/es6-shim.min.js

http://node_modules/systemjs/dist/system-polyfills.js

http://node_modules/angular2/es6/dev/src/testing/shims_for_IE.js

http://node_modules/angular2/bundles/angular2-polyfills.js

http://node_modules/systemjs/dist/system.src.js

http://node_modules/rxjs/bundles/Rx.js

http://node_modules/angular2/bundles/angular2.dev.js

<! Step 3 >

System.config({

packages: {

app: {

format: ‘register’,

defaultExtension: ‘js’

}

}

});

System.import(‘app/main’)

.then(null, console.error.bind(console));

</he

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: JavaJavaScriptGitUTIBM
分页:12
转载请注明
本文标题:Setting up a boilerplate for Angular 2
本站链接:http://www.codesec.net/view/481287.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(30)