MEAN Stack
  • MEAN Stack
  • Getting Started
    • Course Requirements
  • Setup Local Development Environment
    • Git and Gitbash Installation
    • VSCode Installation
    • Node Installation
    • VS Code Live Share
  • Web and Javascript Fundamentals
    • Day One - Intro
    • WK1: Web Fundamentals
      • Let's Git it!
      • HTML, CSS, JS
      • Reading Code & Fixing Errors
        • Challenge 1 - Reading Code & Fixing Errors
        • Challenge: Reading Code and Fixing Errors
      • Homework WK1
    • WK2: JS Versions
      • ES6 and beyond
      • TypeScript
      • Palindrome Challenge
      • Problem Solving
      • Homework WK2
  • Angular
    • WK3: Angular
      • Intro to Angular
        • Angular CLI Install
        • Angular Architecture
          • App Module
          • Modules Explained
          • App Component
          • Angular Files
          • Recap
      • Components & Data Binding
        • Component Boiler Plate
        • Creating a Component
        • Using A Component
        • Component's and CSS Styling
        • Data Binding Challenge
        • User Input with - [(ngModel)]
        • Login Page Challenge
      • Routing
        • Creating A Routing Module
        • Adding Routes
        • Router Outlet Setup
        • Using the Router
        • Routing Challenges
      • Homework WK3
    • WK4: Angular Midterm Project
      • Structural Directives
      • Error Debugging in Angular
        • Tour of Errors Challenge
        • Angular Errors Challenge
      • Intro to Services
      • Midterm Project: Bank Application
    • WK5: Data Flow
      • Intro to API
      • HTTP Requests to API
      • Higher-Order Functions
      • YouTube API
      • Homework WK5
    • Angular Cheat Sheet
  • Backend - Loopback & Mongo
    • WK6: Intro to Backend
      • Announce Final Project
      • Intro to RESTful API
      • Install Loopback
      • Create a MongoDB
      • Connect Loopback to MongoDB
      • Introduce API Explorer
      • Create User Models
    • WK7: Backend Auth & Database Setup
      • Register
      • Login
      • Setup Token Authentication
      • Create Persisted Models
      • Access Control Settings
    • WK8: Final Project Relations
      • Relations
  • Deployment
    • Big Picture
    • Deploying Loopback
      • Learn Environment Variables
      • Using Environment Variables in Loopback
      • Production Environment
      • Deploy to Heroku
        • Create a Heroku App
    • Deploy an Angular App
      • Deploying an Angular App to Netlify
Powered by GitBook
On this page
  • Changing Routes / Views
  • RouterLink
  • Programmatic Router Navigation
  • Using the Router.navigate
  1. Angular
  2. WK3: Angular
  3. Routing

Using the Router

Changing Routes / Views

Once all of the setup is done how do we actually change the route to get a different component view? There are 2 different ways we can do this one that will use HTML's anchor tag, <a> and the other that will use JavaScript, both have their uses.

prerequisites:

RouterLink

The Router Link is used when we want to change the route when the user clicks on link or button. In the below code when a user clicks on a link the proper component is loaded where you placed the <router-outlet></router-outlet>

Keypoint: Router Link is used when changing the view on click.

<ul class="nav navbar-nav">
  <li>
    <a routerLink="about">About</a>
  </li>
  <li>
    <a routerLink="l">Services</a>
  </li>
</ul>

Working Stackblitz Example

Programmatic Router Navigation

We can also navigate or change the view when something happens. For example, we get a successful login response or some other function is invoked we want to change the route. We can use Router Class from angular to change the page. Maybe after 5 seconds a new page is loaded

Keypoint: We can use the Router Class to change the route.

Using the Router.navigate

some.component.ts
import { Router } from '@angular/router';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})

export class someComponent{
}

Line 1: Import the router into your component. This will give you access to the methods on Router

some.component.ts
import { Router } from '@angular/router';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class someComponent{
  constructor( private router: Router ){}
}

Line 4: Add it to the component's constructor

some.component.ts
import { Router } from '@angular/router';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class someComponent{
  constructor( private router: Router ){}
  
  someFunction(){
     this.router.navigate([`/services`])
  }
}

Line 12: We can reference the router and use the navigate method to change to the "services" route.

Working Stack Blitz Example

Key Point:

Navigating inside a function is very useful for cases when you need to change the view after something happens, like say login. We we wouldn't want a user to able to move into our app before we checked their email and password.

PreviousRouter Outlet SetupNextRouting Challenges

Last updated 5 years ago

LogoAngular Router Navigate - StackBlitzStackBlitz
Router.navigate Example
LogoAngular Routing Module - StackBlitzStackBlitz
Sample Router Link