02.Getting Started - nkiateam/angular-tutorial GitHub Wiki
npm μ¬μ©μ μν΄ Node.js λ₯Ό μ€μΉνλ€.
νλ‘μ νΈ ν΄λλ₯Ό μμ±ν ν νλ‘μ νΈ ν΄λμμ package.json μ μμ±νλ€.
$ npm init
μΌλ‘ μ§ννλ©΄ package.json νμΌμ΄ μμ±λλ€.
- package.json μ μλμ κ°μ΄ μμ±νλ€.
{
"name": "angular-tutorial",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"lite": "lite-server",
"tsc": "tsc",
"tsc:w": "tsc -w"
},
"author": "NKIA",
"license": "ISC",
"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"
}
}
μλ npm λͺ λ ΉμΌλ‘ package.json μ μ€μ λ λͺ¨λλ€μ μ€μΉνλ€.
$ npm install
νλ‘μ νΈ ν΄λμ node_modules κ° μμ±λκ³ node_modules ν΄λμμ μ€μΉλ λͺ¨λμ νμΈνλ€.
typescript μ»΄νμΌμ μν΄ νλ‘μ νΈ ν΄λμ tsconfig.json μ μμ±νλ€.
{
"compilerOptions": {
"outDir": "build",
"target": "es5",
"module": "system",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules"
]
}
SystemJS λͺ¨λ λ‘λ μ€μ μ μν΄ νλ‘μ νΈ ν΄λμ systemjs.config.js νμΌμ μμ±νλ€.
(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: './build',
// 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);
"Hello World" λ₯Ό μΆλ ₯νκΈ° μν μμ€λ₯Ό μμ±νλ€. src ν΄λλ₯Ό μμ±νκ³ main.ts, app.module.ts, app.component.ts νμΌμ μμ±νλ€.
- app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<h1>Hello World</h1>'
})
export class AppComponent { }
- 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 { }
- main.ts
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);
νλ‘μ νΈ ν΄λμ index.html νμΌμ μμ±νλ€.
- index.html
<html>
<head>
<title>Angular Tutorial Start</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>
typescript transpileκ³Ό μΉμλ² μ€νμ μν΄ νλ‘μ νΈ ν΄λμμ μλμ κ°μ λͺ λ Ήμ΄λ₯Ό μ€ννλ€.
- typescript λ₯Ό transpile νλ€.
$ npm run tsc
build ν΄λκ° μμ±λκ³ ν΄λμμ transpile λ js νμΌμ΄ μλμ§ νμΈνλ€.
- μΉμλ²λ₯Ό μ€νμν¨λ€.
$ npm run lite
μΉλΈλΌμ°μ μμ http://localhost:3000/ λ‘ "Hello World" κ° μΆλ ₯λλ κ²μ νμΈνλ€.
λλ ν 리 ꡬ쑰λ λ€μκ³Ό κ°λ€.
.
βββ /build/ # The folder for compiled output
βββ /node_modules/ # 3rd-party libraries and utilities
βββ /src/ # The source code of the application
β βββ app.component.ts #
β βββ app.module.ts #
β βββ main.ts #
β
βββ index.html # index.html
βββ systemjs.config.js # SystemJS module loader config file
βββ package.json # The list of 3rd party libraries and utilities
βββ tsconfig.json # TypeScript compiler config file