利用VS Code開發(fā)你的第一個(gè)AngularJS 2應(yīng)用程序
前言
之前已經(jīng)給大家介紹了Angular2開發(fā)環(huán)境搭建教程之VS Code,本文將詳細(xì)介紹利用VS Code如何開發(fā)AngularJS2應(yīng)用程序的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。
運(yùn)行環(huán)境:
1、Windows 10
2、Node 6.7.0

3、npm 3.10.8

4、TypeScript 2.0.3

創(chuàng)建項(xiàng)目
1、創(chuàng)建文件夾:angular2-quickstart,啟動(dòng)VS Code,打開剛創(chuàng)建的文件夾:angular2-quickstart。
2、在根文件夾(angular2-quickstart)下,創(chuàng)建package.json文件:
{
"name": "angular-quickstart",
"version": "1.0.0",
"scripts": {
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"lite": "lite-server",
"postinstall": "typings install",
"tsc": "tsc",
"tsc:w": "tsc -w",
"typings": "typings"
},
"license": "ISC",
"dependencies": {
"@angular/common": "~2.0.2",
"@angular/compiler": "~2.0.2",
"@angular/core": "~2.0.2",
"@angular/forms": "~2.0.2",
"@angular/http": "~2.0.2",
"@angular/platform-browser": "~2.0.2",
"@angular/platform-browser-dynamic": "~2.0.2",
"@angular/router": "~3.0.2",
"@angular/upgrade": "~2.0.2",
"angular-in-memory-web-api": "~0.1.5",
"bootstrap": "^3.3.7",
"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": {
"concurrently": "^3.1.0",
"lite-server": "^2.2.2",
"typescript": "^2.0.3",
"typings": "^1.4.0"
}
}
3、在根文件夾(angular2-quickstart)下,創(chuàng)建tsconfig.json文件:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
}
}
4、在根文件夾(angular2-quickstart)下,創(chuàng)建typings.json文件:
{
"globalDependencies": {
"core-js": "registry:dt/core-js#0.0.0+20160725163759",
"jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
"node": "registry:dt/node#6.0.0+20160909174046"
}
}
5、在根文件夾(angular2-quickstart)下,創(chuàng)建systemjs.config.js(JavaScript腳本)文件:
/**
* 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',
// other libraries
'rxjs': 'npm:rxjs',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
},
// 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'
},
'angular-in-memory-web-api': {
main: './index.js',
defaultExtension: 'js'
}
}
});
})(this);
文件結(jié)構(gòu):
|_ angular2-quickstart |_ app | |_ app.component.ts | |_ main.ts |_ node_modules ... |_ typings ... |_ index.html |_ package.json |_ tsconfig.json |_ typings.json
安裝依賴包(最關(guān)鍵一步)
使用 npm 命令來安裝 package.json 中列出的依賴包。在命令行 cmd 窗口,輸入:cd angular2-quickstart,進(jìn)入angular2-quickstar文件夾下,輸入下列命令:
npm install

創(chuàng)建TypeScript應(yīng)用程序
1、在VS Code中,在根文件夾(angular2-quickstart)下,創(chuàng)建app子文件夾。
2、在子app文件夾下,創(chuàng)建TypeScript文件app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
3、在子app文件夾下,創(chuàng)建TypeScript文件app.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<h1>我的第一個(gè) AngularJS 2 應(yīng)用程序</h1>'
})
export class AppComponent { }
4、在子app文件夾下,創(chuàng)建TypeScript文件main.ts:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);
5、在根文件夾(angular2-quickstart)下,創(chuàng)建html文件index.html:
<html>
<head>
<title>Angular QuickStart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<!-- 1. Load libraries -->
<!-- Polyfill(s) for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<!-- 2. Configure SystemJS -->
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err) {
console.error(err);
});
</script>
</head>
<!-- 3. Display the application -->
<body>
<my-app>Loading...</my-app>
</body>
</html>
6、在根文件夾(angular2-quickstart)下,創(chuàng)建css文件styles.css:
/* Master Styles */
h1 {
color: #369;
font-family: Arial, Helvetica, sans-serif;
font-size: 250%;
}
h2,
h3 {
color: #444;
font-family: Arial, Helvetica, sans-serif;
font-weight: lighter;
}
body {
margin: 2em;
}
配置應(yīng)用程序
1、在VS Code中,在根文件夾(angular2-quickstart)下,創(chuàng)建.vscode子文件夾。
2、在.vscode子文件夾下,創(chuàng)建settings.json文件:
// 將設(shè)置放入此文件中以覆蓋默認(rèn)值和用戶設(shè)置。
{
"typescript.tsdk": "node_modules/typescript/lib",
// ts 項(xiàng)目, 隱藏 .js 和 .js.map 文件
"files.exclude": {
"node_modules": true,
"**/*.js": { "when": "$(basename).ts" },
"**/*.js.map": true
}
}
3、在.vscode子文件夾下,創(chuàng)建tasks.json文件:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "cmd",
"isShellCommand": true,
"showOutput": "always",
"args": ["/C npm start"]
}
運(yùn)行應(yīng)用程序至此,配置完畢,按 Ctrl + Shift + B 編譯,程序?qū)?huì)將Typescript編譯成 Javascript ,同時(shí)啟動(dòng)一個(gè) lite-server, 加載我們編寫的index.html。 顯示:我的第一個(gè) Angular 2 應(yīng)用程序


總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
參考資料:
- Angularjs單選改為多選的開發(fā)過程及問題解析
- AngularJS框架中的雙向數(shù)據(jù)綁定機(jī)制詳解【減少需要重復(fù)的開發(fā)代碼量】
- 基于NodeJS+MongoDB+AngularJS+Bootstrap開發(fā)書店案例分析
- AngularJS開發(fā)教程之控制器之間的通信方法分析
- Ubuntu系統(tǒng)下Angularjs開發(fā)環(huán)境安裝
- 總結(jié)AngularJS開發(fā)者最常犯的十個(gè)錯(cuò)誤
- 利用Angularjs和Bootstrap前端開發(fā)案例實(shí)戰(zhàn)
- AngularJS應(yīng)用開發(fā)思維之依賴注入3
- angularJS開發(fā)注意事項(xiàng)
相關(guān)文章
Angularjs在360兼容模式下取數(shù)據(jù)緩存問題的解決辦法
這篇文章主要為大家詳細(xì)介紹了Angularjs在360兼容模式下取數(shù)據(jù)緩存問題的解決辦法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Angular4學(xué)習(xí)筆記之根模塊與Ng模塊
這篇文章主要介紹了Angular4學(xué)習(xí)筆記之根模塊與Ng模塊,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
requirejs AngularJS結(jié)合使用示例解析
這篇文章主要為大家介紹了requirejs AngularJS結(jié)合使用示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Angular動(dòng)態(tài)綁定樣式及改變UI框架樣式的方法小結(jié)
AngularJS 是一個(gè) JavaScript 框架。它是一個(gè)以 JavaScript 編寫的庫。這篇文章主要介紹了Angular動(dòng)態(tài)綁定樣式及改變UI框架樣式的方法小結(jié),需要的朋友可以參考下2018-09-09
Angularjs注入攔截器實(shí)現(xiàn)Loading效果
angularjs作為一個(gè)全ajax的框架,對(duì)于請(qǐng)求,如果頁面上不做任何操作的話,在結(jié)果反回來之前,頁面是沒有任何響應(yīng)的,不像普通的HTTP請(qǐng)求,會(huì)有進(jìn)度條之類2015-12-12

