02.Getting Started - nkiateam/angular-tutorial GitHub Wiki

Nodejs μ„€μΉ˜

npm μ‚¬μš©μ„ μœ„ν•΄ Node.js λ₯Ό μ„€μΉ˜ν•œλ‹€.

package.json μ„€μ •

ν”„λ‘œμ νŠΈ 폴더λ₯Ό μƒμ„±ν•œ ν›„ ν”„λ‘œμ νŠΈ ν΄λ”μ—μ„œ 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 λͺ¨λ“ˆ μ„€μΉ˜

μ•„λž˜ npm λͺ…λ ΉμœΌλ‘œ package.json 에 μ„€μ •λœ λͺ¨λ“ˆλ“€μ„ μ„€μΉ˜ν•œλ‹€.

$ npm install

ν”„λ‘œμ νŠΈ 폴더에 node_modules κ°€ μƒμ„±λ˜κ³  node_modules ν΄λ”μ—μ„œ μ„€μΉ˜λœ λͺ¨λ“ˆμ„ ν™•μΈν•œλ‹€.

TypeScript 컴파일 μ„€μ •

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 λͺ¨λ“ˆ λ‘œλ” 섀정을 μœ„ν•΄ ν”„λ‘œμ νŠΈ 폴더에 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 좜λ ₯ μ†ŒμŠ€

"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" κ°€ 좜λ ₯λ˜λŠ” 것을 ν™•μΈν•œλ‹€.

Directory Layout

디렉토리 κ΅¬μ‘°λŠ” λ‹€μŒκ³Ό κ°™λ‹€.

.
β”œβ”€β”€ /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
⚠️ **GitHub.com Fallback** ⚠️