ရိုးရိုးလေး ရှင်းပြမယ်
Routing Basics ကို ရိုးရိုးလေးနားလည်မယ်
Angular တွင် RouterModule ဖြင့် single-page application အတွက် client-side routing ကို စီမံခန့်ခွဲသည်။ App project အသစ်ဆောက်စဉ် routing ထည့်မည့် option ကို ရွေးချယ်နိုင်သည်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Wildcard route ကိုအပေါ်မှာထားခြင်းက နောက် routes အားလုံးကိုဖမ်းပြီး page မပေါ်စေနိုင်ပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Angular Router က URL ကို application state အဖြစ်ယူပြီး route configuration နဲ့ component tree ကိုmatch လုပ်ပါတယ်။ Route order, redirects, wildcard, lazy loading နဲ့ navigation history ကိုနားလည်ပြီး shareable/deep-linkable URLs ဖန်တီးပါ။ Page reload, direct URL, browser back/forward နဲ့ not-found behavior များကို feature တစ်ခုလိုစမ်းရပါတယ်။
- Input + state
- Component + template
- Rendered Angular UI
Routing Basics ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
// app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: '**', component: PageNotFoundComponent } // 404 page
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
// app.module.ts
import { AppRoutingModule } from './app-routing.module';
@NgModule({
imports: [BrowserModule, AppRoutingModule]
})Angular Router က URL ကို application state အဖြစ်ယူပြီး route configuration နဲ့ component tree ကိုmatch လုပ်ပါတယ်။ Route order, redirects, wildcard, lazy loading နဲ့ navigation history ကိုနားလည်ပြီး shareable/deep-linkable URLs ဖန်တီးပါ။ Page reload, direct URL, browser back/forward နဲ့ not-found behavior များကို feature တစ်ခုလိုစမ်းရပါတယ်။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Input + state
// app-routing.module.tsAngular တွင် RouterModule ဖြင့် single-page application အတွက် client-side routing ကို စီမံခန့်ခွဲသည်။ App project အသစ်ဆောက်စဉ် routing ထည့်မည့် option ကို ရွေးချယ်နိုင်သည်။
- Component + template
];Angular Router က URL ကို application state အဖြစ်ယူပြီး route configuration နဲ့ component tree ကိုmatch လုပ်ပါတယ်။ Route order, redirects, wildcard, lazy loading နဲ့ navigation history ကိုနားလည်ပြီး shareable/deep-linkable URLs ဖန်တီးပါ။ Page reload, direct URL, browser back/forward နဲ့ not-found behavior များကို feature တစ်ခုလိုစမ်းရပါတယ်။
- Rendered Angular UI
})Home/products/product-detail/not-found routes လေးခုရေးပြီး direct load, invalid URL နဲ့ back navigation ကိုစမ်းပါ။
- Input + state
- Component + template
- Rendered Angular UI
Success
Routing Basics ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
Failure
Wildcard route ကိုအပေါ်မှာထားခြင်းက နောက် routes အားလုံးကိုဖမ်းပြီး page မပေါ်စေနိုင်ပါတယ်။
Angular တွင် RouterModule ဖြင့် single-page application အတွက် client-side routing ကို စီမံခန့်ခွဲသည်။ App project အသစ်ဆောက်စဉ် routing ထည့်မည့် option ကို ရွေးချယ်နိုင်သည်။
// app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: '**', component: PageNotFoundComponent } // 404 page
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
// app.module.ts
import { AppRoutingModule } from './app-routing.module';
@NgModule({
imports: [BrowserModule, AppRoutingModule]
})(Routing module configured ဖြစ်ပြီး navigation လုပ်နိုင်သည်)Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Home/products/product-detail/not-found routes လေးခုရေးပြီး direct load, invalid URL နဲ့ back navigation ကိုစမ်းပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Routing Basics နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
// app-routing.module.ts
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: '**', component: PageNotFoundComponent } // 404 page
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
// app.module.ts
import { AppRoutingModule } from './app-routing.module';
@NgModule({
imports: [BrowserModule, AppRoutingModule]
})ရလဒ်: Routing Basics ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Home/products/product-detail/not-found routes လေးခုရေးပြီး direct load, invalid URL နဲ့ back navigation ကိုစမ်းပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Input + state → Component + template → Rendered Angular UI
- Angular Router က URL ကို application state အဖြစ်ယူပြီး route configuration နဲ့ component tree ကိုmatch လုပ်ပါတယ်။ Route order, redirects, wildcard, lazy loading နဲ့ navigation history ကိုနားလည်ပြီး shareable/deep-linkable URLs ဖန်တီးပါ။ Page reload, direct URL, browser back/forward နဲ့ not-found behavior များကို feature တစ်ခုလိုစမ်းရပါတယ်။
- Wildcard route ကိုအပေါ်မှာထားခြင်းက နောက် routes အားလုံးကိုဖမ်းပြီး page မပေါ်စေနိုင်ပါတယ်။
- Home/products/product-detail/not-found routes လေးခုရေးပြီး direct load, invalid URL နဲ့ back navigation ကိုစမ်းပါ။
Angular — Routing — Angular