๐Ÿงฑ ๊ฐ•์˜ 9B : ์ œํ’ˆ ๊ด€๋ฆฌ (Angular)

๋ฐœํ–‰: (2025๋…„ 12์›” 5์ผ ์˜คํ›„ 02:54 GMT+9)
3 min read
์›๋ฌธ: Dev.to

Source: Dev.to

์†Œ๊ฐœ

์ด ๋ชจ๋“ˆ์€ ์ „์ฒด CRUD ์ž‘์—…๊ณผ ํ”„๋ŸฐํŠธ์—”๋“œ ์ธํ„ฐํŽ˜์ด์Šค์™€ ๋ฐฑ์—”๋“œ API ๊ฐ„์˜ ์›ํ™œํ•œ ํ†ตํ•ฉ์„ ํฌํ•จํ•œ ์™„์ „ํ•œ ์ œํ’ˆ ๊ด€๋ฆฌ ๊ธฐ๋Šฅ์„ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐ ์ค‘์ ์„ ๋‘ก๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๋Š” ์ œํ’ˆ์„ ํšจ์œจ์ ์œผ๋กœ ์ƒ์„ฑ, ์—…๋ฐ์ดํŠธ, ์กฐํšŒ ๋ฐ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์‹œ์Šคํ…œ ์ „๋ฐ˜์— ๊ฑธ์ณ ๋ถ€๋“œ๋Ÿฝ๊ณ  ๋ฐ˜์‘์„ฑ์ด ๋›ฐ์–ด๋‚œ ๊ฒฝํ—˜์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๊ด€๋ฆฌ์ž๋Š” ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

  • ์ œํ’ˆ ๊ด€๋ฆฌ
  • ์ด๋ฏธ ๊ตฌ์ถ•ํ•œ ๋ฐฑ์—”๋“œ API์™€ ํ†ต์‹ 

๋‹ค๋ฃจ๋Š” ์ฃผ์ œ

  • Auth Guard
  • ์ œํ’ˆ์— ๋Œ€ํ•œ ์ „์ฒด CRUD ํŽ˜์ด์ง€
  • Product Service
  • Product Route
  • App Routes ์—…๋ฐ์ดํŠธ
  • ๋ฉ”๋‰ด ์—…๋ฐ์ดํŠธ

Auth Guard

Path: src/app/services/authguard/auth-guard.service.ts

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { AuthApiService } from '../api/auth.service';

@Injectable({
  providedIn: 'root'
})
export class AuthGuardService {
  constructor(
    private authService: AuthApiService,
    private router: Router
  ) {}

  canActivate(): boolean {
    const token = this.authService.getToken();
    const refreshToken = this.authService.getRefreshToken();

    // If no token and no refresh token, redirect to login
    if (!token && !refreshToken) {
      this.router.navigate(['/auth/login']);
      return false;
    }

    // If no token but refresh token exists, try to refresh
    if (!token && refreshToken) {
      this.attemptTokenRefresh(refreshToken);
      return false; // Wait for refresh attempt
    }

    // If token exists, user is authenticated
    if (token) {
      return true;
    }

    return false;
  }

  private attemptTokenRefresh(refreshToken: string): void {
    this.authService.refresh({ token: '', refreshToken }).subscribe({
      next: () => {
        // Token refreshed successfully, reload current route
        this.router.navigate([this.router.url]);
      },
      error: () => {
        // Refresh failed, redirect to login
        this.authService.logout();
        this.router.navigate(['/auth/login']);
      }
    });
  }
}

PrimeNG์„ ์‚ฌ์šฉํ•œ UI ํŽ˜์ด์ง€

์ œํ’ˆ ๊ธฐ๋Šฅ์„ ์œ„ํ•ด ๋‹ค์Œ ์„ธ ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค:

  • src/app/features/products/product/product.html
  • src/app/features/products/product/product.scss (๋นˆ ํŒŒ์ผ)
  • src/app/features/products/product/product.ts

product.html

<div class="card">
  <p-toolbar>
    <ng-template pTemplate="left">
      <button pButton label="Manage Products" class="p-button-success"></button>
    </ng-template>
  </p-toolbar>

  <p-table #dt [value]="products" [paginator]="true" [rows]="10" responsiveLayout="scroll">
    <ng-template pTemplate="header">
      <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Description</th>
        <th>Price</th>
        <th>Stock</th>
      </tr>
    </ng-template>
    <ng-template pTemplate="body" let-product>
      <tr>
        <td>{{ product.id }}</td>
        <td>{{ product.name }}</td>
        <td>{{ product.description }}</td>
        <td>{{ product.price | currency: 'USD' }}</td>
        <td>{{ product.stock }}</td>
      </tr>
    </ng-template>
  </p-table>

  <p-dialog header="Add Product" [(visible)]="displayDialog" [modal]="true" [responsive]="true">
    <div class="p-fluid">
      <div class="p-field">
        <label for="name">Name</label>
        <input id="name" type="text" pInputText [(ngModel)]="product.name" required />
        <small class="p-error">Name is required.</small>
      </div>

      <div class="p-field">
        <label for="description">Description</label>
        <textarea id="description" pInputTextarea [(ngModel)]="product.description"></textarea>
      </div>

      <div class="p-field">
        <label for="price">Price</label>
        <input id="price" type="number" pInputText [(ngModel)]="product.price" />
      </div>

      <div class="p-field">
        <label for="stock">Stock</label>
        <input id="stock" type="number" pInputText [(ngModel)]="product.stock" />
      </div>
    </div>
  </p-dialog>
</div>

product.scss

product.ts

import { Component, OnInit, signal, ViewChild } from '@angular/core';
import { ConfirmationService, MessageService } from 'primeng/api';
import { Table, TableModule } from 'primeng/table';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { ButtonModule } from 'primeng/button';
import { RippleModule } from 'primeng/ripple';
import { ToastModule } from 'primeng/toast';
import { ToolbarModule } from 'primeng/toolbar';
import { RatingModule } from 'primeng/rating';
import { InputTextModule } from 'primeng/inputtext';
import { TextareaModule } from 'primeng/textarea';
import { SelectModule } from 'primeng/select';
import { Radi
Back to Blog

๊ด€๋ จ ๊ธ€

๋” ๋ณด๊ธฐ ยป

Signal Forms ์ž๋™ ์ƒํƒœ ํด๋ž˜์Šค๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค (๊ทธ๋ฆฌ๊ณ  ๋” ๋งŽ์€ ๊ธฐ๋Šฅ)

Reactive Forms vs Signal Forms: ngโ€‘Classes๋Š” ์–ด๋””๋กœ ๊ฐ”๋‚˜์š”? ์—ฌ๊ธฐ Reactive Forms๋กœ ๋งŒ๋“  ํผ์ด ์žˆ์Šต๋‹ˆ๋‹ค: !Reactive Forms๋กœ ๋งŒ๋“  ๊ฐ„๋‹จํ•œ ํผ https://media2...

Angular Signals ์ดํ•ดํ•˜๊ธฐ โ€” Angular์—์„œ ๋ฆฌ์•กํ‹ฐ๋น„ํ‹ฐ์˜ ๋ฏธ๋ž˜

์‹ ํ˜ธ๋ž€ ๋ฌด์—‡์ธ๊ฐ€? ์‹ ํ˜ธ๋Š” ํŠน์ˆ˜ํ•œ reactive variable(๋ฐ˜์‘ํ˜• ๋ณ€์ˆ˜)์ด๋‹ค. ๊ฐ’์ด ๋ณ€ํ•˜๋ฉด Angular๊ฐ€ ์ž๋™์œผ๋กœ UI๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹คโ€”subscriptions(๊ตฌ๋…)๋„, async pipes(๋น„๋™๊ธฐ ํŒŒ์ดํ”„)๋„, boilerplate(๋ณด์ผ๋Ÿฌํ”Œ๋ ˆ์ดํŠธ)๋„ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.

๊ณ ์„ฑ๋Šฅ, ์ ‘๊ทผ์„ฑ, SEO ์ตœ์ ํ™”๋œ ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ Next.js์™€ TypeScript๋กœ ๊ตฌ์ถ•

Said MOUNAIM | ํฌํŠธํด๋ฆฌ์˜ค ๊ณ ์„ฑ๋Šฅ์ด๋ฉฐ ์ ‘๊ทผ์„ฑ์ด ๋›ฐ์–ด๋‚˜๊ณ  SEO ์ตœ์ ํ™”๋œ ํฌํŠธํด๋ฆฌ์˜ค๋กœ, Next.js์™€ TypeScript๋กœ ๊ตฌ์ถ•๋˜์—ˆ์Šต๋‹ˆ๋‹ค. GitHub repo: https://github.com/saidMouna...

์™œ Angular ARIA v21์ด ๊ฝค ๋ฉ‹์ง„๊ฐ€

Angular ARIA๋Š” ์ผ๋ฐ˜์ ์ธ WAIโ€‘ARIA ํŒจํ„ด์„ ๊ตฌํ˜„ํ•˜๋Š” ํ—ค๋“œ๋ฆฌ์Šค์ด๋ฉฐ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ๋””๋ ‰ํ‹ฐ๋ธŒ ๋ชจ์Œ์ž…๋‹ˆ๋‹ค. ์ด ๋””๋ ‰ํ‹ฐ๋ธŒ๋“ค์€ ํ‚ค๋ณด๋“œ ์ธํ„ฐ๋ž™์…˜, ARIA ์†์„ฑ ๋“ฑ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.