The goal of this section is to build on the music search application we created in the previous section and also add pages for Artist, Track and Album listings.
We could implement this app by hiding and showing components on the page either via the
NgIf directive or binding to the
[hidden] property. but then the URL in the address bar doesn’t change and that has a few drawbacks:
Refreshing the page would reset the application back to the start, i.e. if we had performed a search refreshing the page would mean we lose the results.
We cannot bookmark our position in the application and come back to it later.
We cannot send links to other people and have exactly the view show up for them as it shows for us.
Another way to think about the URL in your address bar is that it defines the current state of your application.
State is a computer science terms and means "all the stored information, at a given instant in time, to which program has access".
So the state of an application is the current value of all the variables in the application.
An address in a URL can’t store that much information but it can store enough so that perhaps we can
Giving the a URL to someone else should enable them to bring up the same state in their browser.
In traditional applications built with Server Side Routing when you change the URL in your browser, the browser makes a request to the server to return some HTML which it will display.
However we want to implement something called Client Side Routing. When the URL changes in the browser we want our local application that’s running in the browser (the client) to handle the change, we don’t want the request sent to the server.
There is only ever a single page returned from the server, all further modifications of the page are handled by the client and that’s why it’s called a Single Page Application.
The advantages of an SPA are:
Can be faster. Instead of making a time-consuming request to a far away server every time the URL changes the client app updates the page much faster.
Less bandwidth required. We don’t send over a big html page for every URL change, instead we might just call a smaller API which returns just enough data to render the change in the page.
Convenience. Now a single developer can build most of the functionality of a site instead of splitting the effort between a front end and server side developer.
Angular has a couple of modules which let us implement our application as an SPA, the concept as a whole in Angular is called the Component Router and in this section you will learn how to build an SPA in Angular using the component router.
In this section you will learn:
How to define and configure the different URLs (routes) in your application.
How to navigate between those routes on the client side so the browser doesn’t send a request to the server.
What path location strategies we can use in Angular and the advantages/disadvantages of each.
How to use parametrised routes where part of the URL is a variable.
How to implement nested routes.
How to implement router guards to prevent certain people from accessing certain URLs.