Routing basically controls the flow of our application. It can direct which component would show or where it should be redirected. Let’s look at our basic running app.

1

Previously, there’s a list of stocks that is shown below the header. Now, our goal here is when we put localhost:4200/stocks on the URL, it will redirect us to list of stocks.

First, we need to set our homepage. On your file explorer, click index.html.

2

Next, let’s add the base href which is the file itself(index.html).

3

  • We just added the <base> tag under the head.

Now we are ready to create the routing file. In your file explorer under the src/app folder, create a new file and name it app.routing.ts.

4

5

Open the file and we’ll make a very basic routing class.

6

  • Basically the variable ‘path’ sets the navigation of the route and ‘component’ controls which component to use if the route is called.

Next, go to your app.component.html file. In here we will use our router link.

7

This will show us the list of stocks we defined by clicking the word ‘STOCKS’. Save your work and go back to your browser (provided it is still running via the command ‘ng serve’).

8

Click the word ‘STOCKS’ and the router link should add the extension ‘/stocks’ in the URL.

9

Congratulations! You’ve successfully created a router link.

On the next article, we will be discussing Creating and Using Pipes.

JOIN THE ANGULAR ACADEMY!
$100,000 is the average annual salary of talented software developers and your chances of getting this salary get better by learning Angular 2 today!

Sign up now and make the first step towards upgrading your skill set.

PLUS: Get up to 75% OFF our Angular 2 course in Udemy!
We hate spam. Your email address will not be sold or shared with anyone else.