Simple Example of Angular 8/9 Routing and Navigation
source link: https://www.js-tutorials.com/angularjs-tutorial/simple-example-angular-2-angular-4-routing-navigation/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Simple Example of Angular 8/9 Routing and Navigation
This is another angular 4 tutorial that helps to understand routes and navigation. It’s very easy to create routes in angular 4. Routing help to render the view based on URL parameters. The Router will map the routing url with the component.
Like, We want to render the login page at the URL /login
and homepage at the /home
endpoints, Login page will have LoginComponent for /login
url and a DashboardComponent for the /dashboard
page.
The following steps will follow to create Angular Routing
- We will create Routes file
- We will add RouterOutlet placeholder into the main app.component.html file
- Add navigation link to the page
Angular provides two ways to implement routing in angular 8/9 application:
RouterModule.forRoot
: Used to create routes for the entire application.RouterModule.forChild
: Create routes for lazy loaded modules.
I am assuming, You have created the home and login component, If not please create using Angular CLI:
ng create component login ng create component home
Checkout Other Angular tutorials,
Angular 4 Routing Example
We will create app.routes.ts
inside the src/app
folder, We will import RouterModule
and Routes
from @angular/router
and ModuleWithProviders
from angular/core.
import { ModuleWithProviders } from '@angular/core/src/metadata/ng_module'; import { ModuleWithProviders } from '@angular/core';
We will add routes into app.routes.js
file, The whole file code is like below :
import { RouterModule, Routes } from '@angular/router'; import { ModuleWithProviders } from '@angular/core'; import { LoginComponent } from './login/login.component' import { HomeComponent } from './home/home.component' export const AppRoutes: Routes = [ { path: 'login', component: LoginComponent }, {path: 'home', component: HomeComponent} ]; export const ROUTES: ModuleWithProviders = RouterModule.forRoot(AppRoutes);
The /login
will render LoginComponent and /home
will HomeComponent. The path to specify the URL and Component specify where we want to route.
You can also redirect one url to other view using redirectTo
a directive.
{path: '', redirectTo: 'login', pathMatch: 'full'}
We will add the above app.routes.js file into app.module.ts file with the login and home component,
import { ROUTES } from './app.routes' import { LoginComponent } from './login/login.component' import { HomeComponent } from './home/home.component'
And added ROUTES entry into import array:
imports: [ BrowserModule, FormsModule, ROUTES ],
We need to display view at the time of start app, We will add a placeholder into app.Component.html
that hold the login/home view at the time of the bootstrap application.
The router-outlet tells the Angular router where to display the views.
How to Add Router Link In Angular 4
You can navigate between views using RouterLink
directives. This directive will add to the anchor tag to navigate routes.You can add a navigation link to any page as like below:
import { RouterModule, Routes } from '@angular/router'; import { ModuleWithProviders } from '@angular/core'; import { LoginComponent } from './login/login.component' import { HomeComponent } from './home/home.component' export const AppRoutes: Routes = [ { path: 'login', component: LoginComponent }, {path: 'home', component: HomeComponent} ]; export const ROUTES: ModuleWithProviders = RouterModule.forRoot(AppRoutes);
routerLinkActive
directory help to add active link.
Summary
This angular 8/9 tutorial help to understand the basics of routes and navigation. There are a lot of concepts not cover like nested routes, protected routes, query parameters etc.
You learned how to define a route and navigate through an Angular application. You can also add comments and suggestions to the below comment section.
Recommend
-
57
TheRouter module is one of the most important in the Angular library. Paired withIonic 4, you can be unstoppable. Here’s the rundown: Background One of Ionic 4’s key features is its capability to u...
-
18
In this tutorial series, you'll be provided with a practical introduction to how Next.js can help you build web applications. Created by ZEIT, a cloud company for hosting frontends and...
-
9
A Practical Guide to Angular Share This Article ...
-
6
Angular 8 Tutorial: Routing & Navigation Example by Didin J., updated on Aug 18, 2019 A comprehensive example of Angular 8 Routing & Navigation to help make easier to understanding the Angular Routing con...
-
8
Let’s take another look at routing in Angular—this time how you can use the router link to set up wildcards, so nonexistent URLs bring up a 404 page or redirect users to another page. In this post, we will look into more things you can...
-
3
Angular 13 Tutorial: Routing and Navigation by Didin J., updated on Feb 26, 2022 A...
-
4
The React Mega-Tutorial, Chapter 4: Routing and Page Navigation ...
-
8
Navigation APP example navigation and contact list May 23, 2022 1 min read
-
0
Next.js client-only SPA example Made this example to show how to use Next.js router for a 100% SPA (no JS server) app. You use Next.js router like normally, but don't define getStaticProps and such. Instead yo...
-
2
Modern client-side routing: the Navigation API bookmark_border...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK