Routing Explained
Angular Routing
Routing is used to move from one page to another page in an Angular application.
A single-page application cannot work on one page only, so routing is required.
Routing converts Angular components into pages.
Creating Pages Using Components
Pages are created using Angular components.
All page components are kept inside a pages folder.
Example components created:
- Home
- About
- Login
- Profile
Registering Routes (app.routes.ts)
Components must be registered in the routing file to work as pages.
Each route contains a path and a component.
The empty path ('') is the default route and loads the Home page.
Router Outlet
router-outlet is required to display pages on the UI.
Without it, routing will not work.
Pages are shown exactly where router-outlet is placed.
Creating Page Content (Example)
Each page component contains simple content.
Navigation Using Router Link
routerLink is used instead of href.
It changes pages without reloading the application.
Why Not Use href
Using href reloads the page.
routerLink changes pages without reload, so it is preferred in Angular.