Thuta Learning
Angular
AdvancedWeb Developmentအလယ်အလတ်

Route Parameters

Angularသင်ခန်းစာ 3325 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Route Parameters ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
  • နမူနာ UI ကို run၊ state/output ခန့်မှန်းပြီး edge/error cases စမ်းနိုင်ရန်
  • လက်တွေ့ project တွင် typed safe pattern နဲ့ common pitfalls ကိုခွဲသိနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

Route Parameters ကို ရိုးရိုးလေးနားလည်မယ်

URL parameters များကို access လုပ်ပြီး dynamic routes များ တည်ဆောက်နိုင်သည်။

Problem → Why → Solution

ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ

Snapshot ကိုသာဖတ်ပြီး same component အတွင်း ID ပြောင်းလျှင် data မrefresh ဖြစ်နိုင်ပါတယ်။

ဖြစ်လေ့ရှိတဲ့အမှား

Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်

Route parameters က resource identity, query parameters က optional filters/sort/page, fragment က document location ကိုဖော်ပြသင့်ပါတယ်။ Component reuse ဖြစ်နိုင်လို့ snapshot တစ်ခါဖတ်တာထက် param stream/input binding ကိုလိုအပ်သလိုသုံးပါ။ URL text ကို untrusted input လို့ယူပြီး parse, validate, not-found handling နဲ့ request cancellation ထည့်ပါ။

Mental Model
  1. URL / navigation
  2. Router config + guard
  3. Component view

Route Parameters ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

typescript
// app-routing.module.ts
const routes: Routes = [
  { path: 'user/:id', component: UserDetailComponent },
  { path: 'product/:id/:name', component: ProductComponent }
];

// user-detail.component.ts
import { ActivatedRoute } from '@angular/router';

export class UserDetailComponent implements OnInit {
  userId: string;
  
  constructor(private route: ActivatedRoute) {}
  
  ngOnInit() {
    // Method 1: Snapshot (one-time read)
    this.userId = this.route.snapshot.paramMap.get('id');
    
    // Method 2: Observable (reactive, updates when params change)
    this.route.paramMap.subscribe(params => {
      this.userId = params.get('id');
      this.loadUser(this.userId);
    });
  }
}

// Navigate programmatically
constructor(private router: Router) {}

goToUser(id: number) {
  this.router.navigate(['/user', id]);
}

Route parameters က resource identity, query parameters က optional filters/sort/page, fragment က document location ကိုဖော်ပြသင့်ပါတယ်။ Component reuse ဖြစ်နိုင်လို့ snapshot တစ်ခါဖတ်တာထက် param stream/input binding ကိုလိုအပ်သလိုသုံးပါ။ URL text ကို untrusted input လို့ယူပြီး parse, validate, not-found handling နဲ့ request cancellation ထည့်ပါ။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. URL / navigation// app-routing.module.ts

    URL parameters များကို access လုပ်ပြီး dynamic routes များ တည်ဆောက်နိုင်သည်။

  2. Router config + guardthis.userId = this.route.snapshot.paramMap.get('id');

    Route parameters က resource identity, query parameters က optional filters/sort/page, fragment က document location ကိုဖော်ပြသင့်ပါတယ်။ Component reuse ဖြစ်နိုင်လို့ snapshot တစ်ခါဖတ်တာထက် param stream/input binding ကိုလိုအပ်သလိုသုံးပါ။ URL text ကို untrusted input လို့ယူပြီး parse, validate, not-found handling နဲ့ request cancellation ထည့်ပါ။

  3. Component view}

    `products/:id` page မှာ ID validate, param ပြောင်းတိုင်း request switch, 404 state နဲ့ query tab ကိုsync လုပ်ပါ။

Execution Flow
  1. URL / navigation
  2. Router config + guard
  3. Component view

Success

Route Parameters ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

Failure

Snapshot ကိုသာဖတ်ပြီး same component အတွင်း ID ပြောင်းလျှင် data မrefresh ဖြစ်နိုင်ပါတယ်။

URL parameters များကို access လုပ်ပြီး dynamic routes များ တည်ဆောက်နိုင်သည်။

typescript
// app-routing.module.ts
const routes: Routes = [
  { path: 'user/:id', component: UserDetailComponent },
  { path: 'product/:id/:name', component: ProductComponent }
];

// user-detail.component.ts
import { ActivatedRoute } from '@angular/router';

export class UserDetailComponent implements OnInit {
  userId: string;
  
  constructor(private route: ActivatedRoute) {}
  
  ngOnInit() {
    // Method 1: Snapshot (one-time read)
    this.userId = this.route.snapshot.paramMap.get('id');
    
    // Method 2: Observable (reactive, updates when params change)
    this.route.paramMap.subscribe(params => {
      this.userId = params.get('id');
      this.loadUser(this.userId);
    });
  }
}

// Navigate programmatically
constructor(private router: Router) {}

goToUser(id: number) {
  this.router.navigate(['/user', id]);
}
You should see
(URL မှ parameters များကို read လုပ်ပြီး component တွင် အသုံးပြုနိုင်သည်)

Real-world usage

လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ

`products/:id` page မှာ ID validate, param ပြောင်းတိုင်း request switch, 404 state နဲ့ query tab ကိုsync လုပ်ပါ။

Mini challenge

ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ

Route Parameters နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

  • Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
  • Normal input နဲ့ edge case စမ်းပါ
  • Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
typescript
// app-routing.module.ts
const routes: Routes = [
  { path: 'user/:id', component: UserDetailComponent },
  { path: 'product/:id/:name', component: ProductComponent }
];

// user-detail.component.ts
import { ActivatedRoute } from '@angular/router';

export class UserDetailComponent implements OnInit {
  userId: string;
  
  constructor(private route: ActivatedRoute) {}
  
  ngOnInit() {
    // Method 1: Snapshot (one-time read)
    this.userId = this.route.snapshot.paramMap.get('id');
    
    // Method 2: Observable (reactive, updates when params change)
    this.route.paramMap.subscribe(params => {
      this.userId = params.get('id');
      this.loadUser(this.userId);
    });
  }
}

// Navigate programmatically
constructor(private router: Router) {}

goToUser(id: number) {
  this.router.navigate(['/user', id]);
}

ရလဒ်: Route Parameters ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အရိပ်အမြွက် ကြည့်ရန်

`products/:id` page မှာ ID validate, param ပြောင်းတိုင်း request switch, 404 state နဲ့ query tab ကိုsync လုပ်ပါ။

Quick Quiz 1 / 3

Route Parameters ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • URL / navigation → Router config + guard → Component view
  • Route parameters က resource identity, query parameters က optional filters/sort/page, fragment က document location ကိုဖော်ပြသင့်ပါတယ်။ Component reuse ဖြစ်နိုင်လို့ snapshot တစ်ခါဖတ်တာထက် param stream/input binding ကိုလိုအပ်သလိုသုံးပါ။ URL text ကို untrusted input လို့ယူပြီး parse, validate, not-found handling နဲ့ request cancellation ထည့်ပါ။
  • Snapshot ကိုသာဖတ်ပြီး same component အတွင်း ID ပြောင်းလျှင် data မrefresh ဖြစ်နိုင်ပါတယ်။
  • `products/:id` page မှာ ID validate, param ပြောင်းတိုင်း request switch, 404 state နဲ့ query tab ကိုsync လုပ်ပါ။

Angular — Routing — Angular

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Snapshot ကိုသာဖတ်ပြီး same component အတွင်း ID ပြောင်းလျှင် data မrefresh ဖြစ်နိုင်ပါတယ်။
  • Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

လက်တွေ့လေ့ကျင့်ခန်း

Route Parameters နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: (URL မှ parameters များကို read လုပ်ပြီး component တွင် အသုံးပြုနိုင်သည်)

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Route Parameters | Thuta Learning