Podcast Title

Author Name

0:00
0:00
Album Art
root@10xdev:~$
Type commands: ls ebooks | ls roadmaps | ls podcasts | ls apps | ls posts | ls telegram | help | clear
user@10xdev:~$ welcome to 10xdev.blog
user@10xdev:~$

🧠 이 ν•œ 쀄 λ³€κ²½μœΌλ‘œ Angular 20 라우트 νŒŒλΌλ―Έν„° 바인딩이 λͺ…ν™•ν•΄μ§„λ‹€

By Ahmed Bouchefra June 18, 2025
Listen

AngularλŠ” μ• ν”Œλ¦¬μΌ€μ΄μ…˜ λ‚΄ 라우트 νŒŒλΌλ―Έν„° 처리 λ°©μ‹μ—μ„œ λ†€λΌμš΄ λ°œμ „μ„ 보여주고 μžˆμŠ΅λ‹ˆλ‹€. Angular 16 이전 λ²„μ „μœΌλ‘œ μž‘μ—…ν•΄ λ³Έ 개발자라면 λ‹¨μˆœν•œ id ν•˜λ‚˜λ₯Ό μΆ”μΆœν•˜κΈ° μœ„ν•΄ ActivatedRoute와 paramMap을 반볡적으둜 μ‚¬μš©ν•œ 기얡이 μžˆμ„ κ²ƒμž…λ‹ˆλ‹€.

ν•˜μ§€λ§Œ 이제 Angular 20의 μ΅œμ‹  개발 νŒ¨ν„΄κ³Ό 잘 μ–΄μšΈλ¦¬λŠ” 더 λ‚˜μ€ 방법이 μžˆμŠ΅λ‹ˆλ‹€. λ°”λ‘œ withComponentInputBinding()μž…λ‹ˆλ‹€.

이 κΈ€μ—μ„œλŠ” 이 κΈ°λŠ₯이 무엇인지, μ–΄λ–€ 문제λ₯Ό μ•”λ¬΅μ μœΌλ‘œ μ΄ˆλž˜ν•˜λŠ”μ§€, 그리고 이λ₯Ό μš°μ•„ν•˜κ²Œ ν•΄κ²°ν•˜λŠ” 방법을 μ†Œκ°œν•©λ‹ˆλ‹€ β€” Angular 20 κΈ°μ€€μœΌλ‘œ μ„€λͺ…ν•©λ‹ˆλ‹€.


πŸ’‘ withComponentInputBinding()μ΄λž€?

withComponentInputBinding()은 라우트 νŒŒλΌλ―Έν„°λ₯Ό μ»΄ν¬λ„ŒνŠΈμ˜ @Input() 속성에 μžλ™μœΌλ‘œ 바인딩할 수 μžˆλ„λ‘ ν•˜λŠ” μ„€μ • μœ ν‹Έλ¦¬ν‹°μž…λ‹ˆλ‹€.

이 κΈ°λŠ₯을 μ‚¬μš©ν•˜λ©΄ 라우트 μ˜΅μ €λ²„λΈ”μ„ μˆ˜λ™μœΌλ‘œ ꡬ독할 ν•„μš” 없이 μ»΄ν¬λ„ŒνŠΈμ—μ„œ νŒŒλΌλ―Έν„°λ₯Ό λ°”λ‘œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄, /user/:idλΌλŠ” κ²½λ‘œκ°€ 있고 μ»΄ν¬λ„ŒνŠΈμ— idλΌλŠ” @Input()이 μžˆλ‹€λ©΄, AngularλŠ” μžλ™μœΌλ‘œ 이λ₯Ό λ§€ν•‘ν•΄μ€λ‹ˆλ‹€ β€” 단, λΌμš°ν„° μ„€μ •μ—μ„œ 이 κΈ°λŠ₯을 ν™œμ„±ν™”ν–ˆμ„ κ²½μš°μ—λ§Œ.

βœ… μ‚¬μš© 방법은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€:

  1. 경둜λ₯Ό μ •μ˜ν•©λ‹ˆλ‹€:
{ path: 'user/:id', component: UserComponent }
  1. μ»΄ν¬λ„ŒνŠΈμ—μ„œ @Input()을 μ‚¬μš©ν•©λ‹ˆλ‹€:
@Input() id!: string;
  1. λΌμš°ν„° μ„€μ •μ—μ„œ κΈ°λŠ₯을 ν™œμ„±ν™”ν•©λ‹ˆλ‹€:
provideRouter(routes, withComponentInputBinding())

이제 더 이상 ꡬ독 없이도 라우트 νŒŒλΌλ―Έν„°κ°€ μ»΄ν¬λ„ŒνŠΈμ— μžλ™ μ „λ‹¬λ©λ‹ˆλ‹€.


πŸ˜• 문제점: μ½”λ“œμ—μ„œμ˜ μ˜λ„ λͺ¨ν˜Έμ„±

이 κΈ°λŠ₯은 λΆ„λͺ… μœ μš©ν•˜μ§€λ§Œ, μ½”λ“œμ˜ μ˜λ„λ₯Ό 흐릴 수 μžˆλŠ” λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€.

@Input()은 일반적으둜 λΆ€λͺ¨ μ»΄ν¬λ„ŒνŠΈλ‘œλΆ€ν„° 데이터λ₯Ό μ „λ‹¬λ°›λŠ”λ‹€λŠ” 의미둜 μ‚¬μš©λ©λ‹ˆλ‹€. 그런데 여기에 라우트 νŒŒλΌλ―Έν„°λ₯Ό μ—°κ²°ν•˜λ©΄, μ½”λ“œμ˜ μ˜λ―Έκ°€ μ• λ§€ν•΄μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.

λ‹€λ₯Έ 개발자(ν˜Ήμ€ 미래의 μžμ‹ )λŠ” 이 값이 URLμ—μ„œ μ™”λ‹€λŠ” 사싀을 νŒŒμ•…ν•˜μ§€ λͺ»ν•˜κ³ , λΆ€λͺ¨ μ»΄ν¬λ„ŒνŠΈμ—μ„œ μ „λ‹¬λ˜μ—ˆλ‹€κ³  착각할 수 μžˆμŠ΅λ‹ˆλ‹€.


πŸ§ͺ ν•΄κ²° 방법: λͺ…ν™•ν•œ μ˜λ„λ₯Ό μœ„ν•œ 별칭 μ‚¬μš©

해결책은 κ°„λ‹¨ν•©λ‹ˆλ‹€. Angular의 λ‚΄μž₯ input() μ‹œκ·Έλ„μ— 별칭을 λΆ™μ—¬, 예λ₯Ό λ“€μ–΄ routeBinding()μ΄λΌλŠ” μ΄λ¦„μœΌλ‘œ μ‚¬μš©ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

import { input, input as routeBinding } from '@angular/core';

이제 λ‹€μŒκ³Ό 같이 μž…λ ₯의 좜처λ₯Ό λͺ…ν™•νžˆ ꡬ뢄할 수 μžˆμŠ΅λ‹ˆλ‹€:

  • input() β†’ λΆ€λͺ¨ μ»΄ν¬λ„ŒνŠΈμ—μ„œ μ „λ‹¬λœ κ°’
  • routeBinding() β†’ λΌμš°ν„°μ—μ„œ μ „λ‹¬λœ κ°’

μ˜ˆμ‹œ:

id = routeBinding<string>();     // λΌμš°ν„°μ—μ„œ μ „λ‹¬λœ νŒŒλΌλ―Έν„°
title = input<string>();         // λΆ€λͺ¨ μ»΄ν¬λ„ŒνŠΈμ—μ„œ μ „λ‹¬λœ μž…λ ₯κ°’

이 μž‘μ€ 변경은 Angular의 μž‘λ™ 방식에 μ•„λ¬΄λŸ° 영ν–₯을 μ£Όμ§€ μ•Šμ§€λ§Œ, μ½”λ“œμ˜ μ˜λ„λ₯Ό λͺ…ν™•νžˆ ν•˜κ³ , 가독성과 μœ μ§€λ³΄μˆ˜λ₯Ό κ°œμ„ ν•©λ‹ˆλ‹€.


🧱 μ˜ˆμ‹œ: Angular 20μ—μ„œ λͺ…ν™•ν•œ 라우트 νŒŒλΌλ―Έν„° 바인딩

πŸ”Ή 1단계: UserComponent 생성

import { Component, input, input as routeBinding } from '@angular/core';

@Component({
  selector: 'app-user',
  standalone: true,
  template: `
    <h2>μœ μ € μ»΄ν¬λ„ŒνŠΈ</h2>
    <p>라우트 νŒŒλΌλ―Έν„° ID: <strong></strong></p>
  `,
})
export class UserComponent {
  id = routeBinding<string>(); // λΌμš°ν„°μ—μ„œ 전달
  regularInput = input<string>(); // λΆ€λͺ¨μ—μ„œ 전달
}

πŸ”Ή 2단계: 루트 μ»΄ν¬λ„ŒνŠΈ μ„€μ •

import { Component } from '@angular/core';
import { RouterLink, RouterOutlet } from '@angular/router';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [RouterOutlet, RouterLink],
  template: `
    <h1>Angular 20 라우트 νŒŒλΌλ―Έν„° 데λͺ¨</h1>
    <nav>
      <a [routerLink]="['/user', 42]">/user/42 이동</a>
    </nav>
    <router-outlet/>
  `,
})
export class AppComponent {}

πŸ”Ή 3단계: λΌμš°ν„° μ„€μ • 및 μ•± λΆ€νŠΈμŠ€νŠΈλž©

import { bootstrapApplication } from '@angular/platform-browser';
import { provideRouter, Routes, withComponentInputBinding } from '@angular/router';
import { AppComponent } from './app.component';
import { UserComponent } from './user.component';

const routes: Routes = [
  {
    path: 'user/:id',
    component: UserComponent,
  },
];

bootstrapApplication(AppComponent, {
  providers: [provideRouter(routes, withComponentInputBinding())],
});

βœ… μš”μ•½

| κΈ°λŠ₯ | μ„€λͺ… | | ----------------------------- | ----------------------------------- | | withComponentInputBinding() | 라우트 νŒŒλΌλ―Έν„°λ₯Ό μ»΄ν¬λ„ŒνŠΈ μž…λ ₯에 μžλ™ μ—°κ²° | | 별칭 μ‚¬μš© | routeBinding() 별칭을 μ‚¬μš©ν•˜μ—¬ μ½”λ“œ μ˜λ„ λͺ…ν™•ν™” | | Angular 20 μ™„λ²½ ν˜Έν™˜ | μŠ€νƒ λ“œμ–Όλ‘  μ»΄ν¬λ„ŒνŠΈ 및 μ‹œκ·Έλ„ μ•„ν‚€ν…μ²˜μ— 졜적 | | DX ν–₯상 | 더 μ‰¬μš΄ 디버깅과 ν˜‘μ—… κ°€λŠ₯ |


πŸš€ 마무리 생각

비둝 withComponentInputBinding()은 이전 버전에 λ„μž…λœ κΈ°λŠ₯μ΄μ§€λ§Œ, Angular 20은 이 κΈ°λŠ₯을 λ„μž…ν•˜κΈ°μ— μ™„λ²½ν•œ μ‹œμ μž…λ‹ˆλ‹€. μŠ€νƒ λ“œμ–Όλ‘  μ»΄ν¬λ„ŒνŠΈμ™€ μ‹œκ·Έλ„ 쀑심 μ•„ν‚€ν…μ²˜κ°€ 쀑심이 된 μ§€κΈˆ, λΌμš°νŒ… 방식도 ν•œ 단계 μ§„ν™”ν•  ν•„μš”κ°€ μžˆμŠ΅λ‹ˆλ‹€.

input()을 라우트 νŒŒλΌλ―Έν„°μš©μœΌλ‘œ aliasν•˜μ—¬ μ‚¬μš©ν•˜λŠ” 이 μž‘μ€ νŠΈλ¦­μ€ μ½”λ“œ μ˜λ„λ₯Ό λͺ…ν™•νžˆ ν•˜κ³ , ν˜‘μ—…κ³Ό μœ μ§€λ³΄μˆ˜λ₯Ό μ‰½κ²Œ λ§Œλ“­λ‹ˆλ‹€.

πŸ’¬ μ—¬λŸ¬λΆ„μ˜ 생각은?

이 별칭 νŒ¨ν„΄μ„ Angular 20 ν”„λ‘œμ νŠΈμ— λ„μž…ν•΄λ³΄μ‹œκ² μŠ΅λ‹ˆκΉŒ? Angularμ—μ„œ κ³΅μ‹μ μœΌλ‘œ @RouteParam() 같은 λ°μ½”λ ˆμ΄ν„°λ₯Ό λ„μž…ν•˜λ©΄ μ’‹μ„κΉŒμš”?

Join the 10xdev Community

Subscribe and get 8+ free PDFs that contain detailed roadmaps with recommended learning periods for each programming language or field, along with links to free resources such as books, YouTube tutorials, and courses with certificates.

Recommended For You

Up Next