Angular 7 - Angular Routes


Angular Routes



Today i am going to tell you about Angular Routing and I will show you how to navigate the application with router links.Let's start learning Angular Routing.
  • The Angular Router is used to navigate from one view to the next view when users perform certain application task.
  • We normally use browser for navigation.We use browser for navigation as follows.
  1.  Enter a URL in the address bar and  navigates to a required page.
  2. Click links on the web page and navigates to a new page.
  3. Click the browser's back and forward buttons and navigates by using browser history.
  • Angular Routing also based on above browser model.It used URL as an instruction to navigate to a view. When we bind the router to links on a page and it will navigate to the appropriate application or component view when the user clicks a link.
  •  Also we can navigate users when they clicks a button. Also the router logs activities in the browser's history .Therefore we can use back and forward buttons for navigation.

How to apply routing to your application

Step 01

Angular Router is not a part of the Angular core and in its own library package, @angular/router. Therefore we have to Import router library in our app.module.ts file which is in app component to start routing.


Step 02

We should import the RouterModule.forRoot() method under the  AppModule's imports array to configure the router. Until we configure the router there are no routes in our application.



I have created my application as follows and i want to display relevant component in the empty space when i click the links on the navigation bar.


Step 03


To retrieve required view when we click on the link we needed to put router-outlet tag within needed space as below.

 
Step 04

As next step we should create route definitions in the app.module.ts file as follows.For the path you should put the word which is appeared after the "/" (root) and  empty path load the default component.As the component you should put component name correctly which is imported in the app.module.ts file.


 
Step 05

As final step we should set the link paths using routerLink within anchor tag instead of href.


After the above steps we can use our application as follows.

eg: when i clicked attendance link which is on navigation bar it loaded the attendance-overview component in the empty space .


Comments

Post a Comment

Popular posts from this blog

Angular 6 - Data Binding Part 2

Machine Learning

Selenium - 1