Components - nkiateam/angular-tutorial GitHub Wiki

components

์ปดํฌ๋„ŒํŠธ๋Š” ํ™”๋ฉด์˜ ์ผ๋ถ€๋ฅผ ์ด๋ฃจ๋Š” View๋ฅผ ์ œ์–ดํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, ์˜ˆ์ œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋‹ค์Œ์˜ View๋“ค์€ ์ปดํฌ๋„ŒํŠธ์— ์˜ํ•ด ์ œ์–ด๋ฉ๋‹ˆ๋‹ค.

  • ๋„ค๋น„๊ฒŒ์ด์…˜ ๋งํฌ๋ฅผ ๊ฐ€์ง„ App root
  • ์˜์›…๋“ค์˜ ๋ชฉ๋ก(List of Heroes)
  • ์˜์›… ํŽธ์ง‘๊ธฐ(Hero Editor)

View๋ฅผ ์ œ๊ณตํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹น์‹ ์€ ํด๋ž˜์Šค ์•ˆ์— ์ปดํฌ๋„ŒํŠธ์˜ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ์ง์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ํด๋ž˜์Šค๋Š” ํ”„๋กœํผํ‹ฐ์™€ ๋ฉ”์†Œ๋“œ์˜ API๋ฅผ ํ†ตํ•˜์—ฌ View์™€ ์ƒํ˜ธ ์ž‘์šฉํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ์˜ HeroListComponent๋Š” ์„œ๋น„์Šค๋กœ๋ถ€ํ„ฐ ์–ป์–ด์ง„ ์˜์›…๋“ค์˜ ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” heroes ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ๋ชฉ๋ก์œผ๋กœ๋ถ€ํ„ฐ ํ•˜๋‚˜์˜ ์˜์›…์„ ์„ ํƒํ•˜์—ฌ ํด๋ฆญํ–ˆ์„ ๋•Œ, HeroListComponent๋Š” selectedHero ํ”„๋กœํผํ‹ฐ๋ฅผ ์„ธํŒ…ํ•˜๋Š” selectHero()๋ผ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

export class HeroListComponent implements OnInit {
  heroes: Hero[];
  selectedHero: Hero;

  constructor(private service: HeroService) { }

  ngOnInit() {
    this.heroes = this.service.getHeroes();
  }

  selectHero(hero: Hero) { this.selectedHero = hero; }
}

์‚ฌ์šฉ์ž๊ฐ€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‚ฌ์šฉํ•จ์— ๋”ฐ๋ผ Angular๋Š” ๊ทธ์— ๋งž๋Š” ์ปดํฌ๋„ŒํŠธ๋“ค์„ ์ƒ์„ฑํ•˜๊ณ , ๊ฐฑ์‹ , ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ์˜ˆ์ œ์—์„œ ์„ ์–ธ๋œ ngOnInit()์ฒ˜๋Ÿผ ์‚ฌ์šฉ์ž์˜ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์„ ํƒ์ ์ธ lifecycle hooks๋ฅผ ํ†ตํ•˜์—ฌ ๊ฐ ์‹œ์ ์˜ ์ƒ๋ช…์ฃผ๊ธฐ ์•ˆ์—์„œ ํ–‰๋™์„ ์ทจํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.