Modules - nkiateam/angular-tutorial GitHub Wiki

Modules

Angular App๋Š” ๋ชจ๋“ˆํ˜•์œผ๋กœ ๋˜์–ด์žˆ๊ณ  Angular ๋ชจ๋“ˆ ๋˜๋Š” NgModules์ด๋ผ๋Š” ๋ถˆ๋ฆฌ๋Š” ์ž์ฒด ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

Angular ๋ชจ๋“ˆ์€ ๋ถ„๋Ÿ‰์ด ๋งŽ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ํŽ˜์ด์ง€์—์„œ๋Š” ๋ชจ๋“ˆ์„ ์†Œ๊ฐœํ•˜๊ณ . ADVENCED > Angular Modules (NgModule) ํŽ˜์ด์ง€์—์„œ ์ข€ ๋” ๊นŠ๊ฒŒ ๋‹ค๋ฃฉ๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ชจ๋“  Angular App์€ ๊ธฐ์กด์— AppModule์ด๋ผ๋Š” ๋ฃจํŠธ ๋ชจ๋“ˆ์„ ํ•˜๋‚˜ ์ด์ƒ ๋ณด์œ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฃจํŠธ ๋ชจ๋“ˆ์€ ์ž‘์€ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์—์„œ๋Š” ํ•œ ๊ฐœ์˜ ๋ชจ๋“ˆ๋กœ ๊ตฌ์„ฑ๋  ์ˆ˜ ์žˆ์ง€๋งŒ, ๋Œ€๋ถ€๋ถ„์˜ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์—๋Š” ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ๋„๋ฉ”์ธ, ์›Œํฌ ํ”Œ๋กœ์šฐ ๋˜๋Š” ๋ฐ€์ ‘ํ•œ ๊ด€๊ณ„์˜ ๊ธฐ๋Šฅ ๋“ฑ์„ ๊ธฐ์ค€์œผ๋กœ ๊ฒฐํ•ฉ๋œ ์ฝ”๋“œ๋“ค์ด ๊ฐ๊ฐ ๋ชจ๋“ˆ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค.

Angular ๋ชจ๋“ˆ์ด๋ผ ํ•จ์€ ๋ฃจํŠธ๋‚˜ ๊ธฐ๋Šฅ ์—ฌ๋ถ€์— ์ƒ๊ด€์—†์ด @NgModule decorator๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.

๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ผ๊ณ  ํ•จ์€ JavaScript ํด๋ž˜์Šค๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. Angular๋Š” ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๋ฅผ ํด๋ž˜์Šค์— ์ฒจ๋ถ€ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๊ฐ€ ๋งŽ์ด ์žˆ์œผ๋ฏ€๋กœ ํด๋ž˜์Šค์˜ ์˜๋ฏธ์™€ ์ž‘๋™ ๋ฐฉ์‹์„ ์•Œ๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์›น์˜ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด์‹ญ์‹œ์˜ค.

NgModule์€ ๋ชจ๋“ˆ์˜ ์†์„ฑ์„ ์„ค๋ช…ํ•˜๋Š” ๋‹จ์ผ ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์ค‘์š”ํ•œ ์†์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

  • declarations - ๋ทฐ ํด๋ž˜์Šค์— ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“ˆ. Angular ์—์„œ๋Š” components, directives, and pipes ์„ธ๊ฐ€์ง€ ๋ทฐ ํด๋ž˜์Šค๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • exports - ๋‹ค๋ฅธ ๋ชจ๋“ˆ์˜ ์ปดํฌ๋„ŒํŠธ ํ…œํ”Œ๋ฆฟ์—์„œ ํ‘œ์‹œํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ์„ ์–ธ์˜ ๋ถ€๋ถ„ ์ง‘ํ•ฉ.

  • imports - ๋‹ค๋ฅธ ๋ชจ๋“ˆ์—์„œ export๋œ ํด๋ž˜์Šค๋Š” ํ•ด๋‹น ๋ชจ๋“ˆ์—์„œ ์ปดํฌ๋„ŒํŠธ ํ…œํ”Œ๋ฆฟ ์„ ์–ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

  • providers - Service์˜ ์ƒ์„ฑ์ž๋Š” ์„œ๋น„์Šค๋ฅผ์˜ ๋ชจ๋“  ์ „์—ญ ์ฝœ๋ ‰์…˜์œผ๋กœ ์ƒ์„ฑํ•˜์—ฌ App์˜ ๋ชจ๋“  ๋ถ€๋ถ„์—์„œ ์•ก์„ธ์Šค ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ฉ๋‹ˆ๋‹ค.

  • bootstrap - ๋ฃจํŠธ ์ปดํฌ๋„ŒํŠธ๋ผ ๋ถˆ๋ฆฌ๋Š” ๋ฉ”์ธ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ทฐ๋Š” ๋‹ค๋ฅธ ๋ชจ๋“  App์˜ ๋ทฐ๋ฅผ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์˜ค์ง ๋ฃจํŠธ ๋ชจ๋“ˆ ๋งŒ์ด ๋ถ€ํŠธ์ŠคํŠธ๋žฉ์˜ ์†์„ฑ์„ ์„ค์ • ํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Here์˜ ๊ฐ„๋‹จํ•œ ๋ฃจํŠธ ๋ชจ๋“ˆ:

app/app.module.ts
COPY CODE
import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
@NgModule({
  imports:      [ BrowserModule ],
  providers:    [ Logger ],
  declarations: [ AppComponent ],
  exports:      [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

์œ„์˜ AppComponent์˜ Export๋Š” ์–ด๋–ป๊ฒŒ Export๋˜๋Š”์ง€ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” ์ด ์˜ˆ์ œ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค. ๋ฃจํŠธ ๋ชจ๋“ˆ์€ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฃจํŠธ๋ชจ๋“ˆ์„ import ํ•˜์ง€ ์•Š์•„๋„ ๋˜๊ธฐ๋•Œ๋ฌธ์— ์•„๋ฌด๊ฒƒ๋„ ๋‚ด๋ณด ๋‚ผ ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

๋ฃจํŠธ ๋ชจ๋“ˆ์„ ๋ถ€ํŠธ ์ŠคํŠธ๋žฉํ•˜์—ฌ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ์‹œ์ž‘ํ•˜์‹ญ์‹œ์˜ค. ๊ฐœ๋ฐœํ•˜๋Š” ๋™์•ˆ AppModule์—์„œ main.ts ํŒŒ์ผ๋กœ ๋ถ€ํŠธ ์ŠคํŠธ๋žฉ ํ•  ์ˆ˜ ํ•„์š”๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

app/main.ts
COPY CODE
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule);

####Angular modules vs. JavaScript modules The Angular module โ€” @NgModule ๋กœ ๋ฐ์ฝ”๋ ˆ์ดํŠธ๋œ ํด๋ž˜์Šค โ€” ๋Š” Angular์˜ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.

JavaScript๋Š” JavaScript ๊ฐ์ฒด์˜ ์ปฌ๋ ‰์…˜์„ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์ž์ฒด ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์„ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ Angular์˜ ๋ชจ๋“ˆ์‹œ์Šคํ…œ๊ณผ๋Š” ์™„์ „ํžˆ ๋‹ค๋ฅธ ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

In JavaScript each file is a module and all objects defined in the file belong to that module. The module declares some objects to be public by marking them with the export key word. Other JavaScript modules use import statements to access public objects from other modules. JavaScript์—์„œ ๊ฐ ํŒŒ์ผ์€ ๋ชจ๋“ˆ์ด๋ฉฐ ํŒŒ์ผ์— ์ •์˜๋œ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ํ•ด๋‹น ๋ชจ๋“ˆ์— ์†ํ•ฉ๋‹ˆ๋‹ค. ๋ชจ๋“ˆ์€ ์ผ๋ถ€ ๊ฐ์ฒด๋ฅผ ์„ ์–ธํ•˜๊ธฐ ์œ„ํ•˜์—ฌ public์œผ๋กœ ํ‘œ์‹œํ•˜๊ณ  ํ‚ค์›Œ๋“œ๋ฅผ export ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ JavaScript ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด import๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋‹ค๋ฅธ ๋ชจ๋“ˆ์˜ ๊ณต์šฉ ๊ฐ์ฒด์— ์•ก์„ธ์Šคํ•ฉ๋‹ˆ๋‹ค.

import { NgModule }     from '@angular/core';
import { AppComponent } from './app.component';
export class AppModule { }

Web์ƒ์—์„œ์˜ ์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์— ๋Œ€ํ•ด ์ž์„ธํžˆ ์•Œ์•„๋ณด์‹ญ์‹œ์˜ค.

์ด ๋‘๊ฐ€์ง€๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ๋ณด์™„์ ์ธ ๋ชจ๋“ˆ์ž…๋‹ˆ๋‹ค. ๋‘๊ฐ€์ง€ ๋ชจ๋‘๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์•ฑ์„ ์ž‘์„ฑํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

####Angular libraries Angular๋Š” JavaScript ๋ชจ๋“ˆ์˜ ์ปฌ๋ ‰์…˜์˜ ๋ชจ์Œ์ž…๋‹ˆ๋‹ค. ๋‹น์‹ ์˜ ์ด๊ฒƒ๋“ค์„ ๋งˆ์น˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ๋“ˆ์ฒ˜๋Ÿผ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ Angular Library์˜ ์ด๋ฆ„์€ @angular์ด๋ž€ ์ ‘๋‘์–ด๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

npm ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋“ค์„ ์„ค์น˜ํ•˜๊ณ , JavaScript import๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ ์ค‘ ์ผ๋ถ€๋ฅผ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด Angular์˜ Component ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋ฅผ @ angular / core ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

import { Component } from '@angular/core';

JavaScript import ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ Angular ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ Angular ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

import { BrowserModule } from '@angular/platform-browser';

In the example of the simple root module above, the application module needs material from within that BrowserModule. To access that material, add it to the @NgModule metadata imports like this. ์œ„์˜ ๊ฐ„๋‹จํ•œ root ๋ชจ๋“ˆ ์˜ˆ์ œ์—์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ชจ๋“ˆ์€ ํ•ด๋‹น BrowserModule ๋‚ด๋ถ€์˜ ๋‚ด์šฉ์„ ํ•„์š”๋กœ ํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ž๋ฃŒ์— ์•ก์„ธ์Šคํ•˜๋ ค๋ฉด @NgModule์˜ ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ์— import๋ฅผ ์ถ”๊ฐ€ํ•˜์‹ญ์‹œ์˜ค.

imports:      [ BrowserModule ],

์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ Angular์™€ JavaScript ๋ชจ๋“ˆ ์‹œ์Šคํ…œ์„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‘ ์‹œ์Šคํ…œ์€ "imports"์™€ "exports"๋ผ๋Š” ๊ณตํ†ต ๋ช…๋ น์–ด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ˜ผ๋™ํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํ˜ผ๋™์Šค๋Ÿฌ์šด ๋ฌธ์ œ๋Š” ์‹œ๊ฐ„๊ณผ ๊ฒฝํ—˜์„ ํ†ตํ•ด์„œ๋งŒ ํ•ด๊ฒฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

Angular Module ํŽ˜์ด์ง€์—์„œ ์ž์„ธํ•˜๊ธฐ ์•Œ์•„๋ณด์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.