In this lecture, we going to add static data to our roots.
Data can be associated with a root.
They data we add to a root can be helpful for adding additional information about a root.
Let's look at a practical example in the previous lecture.
We redirected the user to the home page whenever they log out of the application.
It works, but it's not perfect.
The user is always being redirected, which is a behavior we may not want if they're on the about page.
I don't see a reason as to why they should be redirected.
The about page is accessible to all users.
We should redirect the user if the route requires authentication.
Our issue can be resolved by adding data to our roots.
Let's explore how that's done.
Open the video routing module.
In the round object for the manage routes, we will add a new object called data.
Inside this object, we can add static data to a route.
This data will be exposed to other areas of our app by the router services.
For example, we can add data to this route to specify if this route requires authentication inside
this object.
We will add a property called off only with an initial value of true.
We don't need to have this same object to other routes.
If the off only property is missing from other routes, we can safely assume the route doesn't need
authentication.
We can let users remain on the page there.
On the next step is to retrieve data from the service.
I wish I could say this is easy, but angular makes this task difficult.
Let me show you why open the manage component class?
At the top of the file, we are going to import a class called activated roots.
The activated route class is a service we can inject into our components to gather information about
the route the user is currently on.
It's not to be confused with the route or service which provides methods for interacting with the router.
Let's inject it into the component through the constructor function.
We will refer to the service as route.
Next, inside the nji on init function, we will subscribe to the this route data property.
The data, property stores and observable that pushes the data from the current route, we can subscribe
to this observable to grab the data we added to our roots inside the Subscribe function.
We will pass in the console log function.
Next, let's refresh the Manage page in the browser with the console opens.
If we look in the console, we will find the object has been logged.
It contains the op only property.
Everything works as expected.
So what's the problem?
The router does not make the data easily accessible to services or components to find outside of the
router outlet directive.
Keep in mind the roundel outlet DIRECTV will manage the components for root components managed by this
directive will be given the latest changes to a root.
The story is different outside of the directive.
Let's go back to our editor.
The manage component doesn't need the data.
The purpose of importing the route data was to examine it in the console.
Typically, you will want to access data in this manner.
However, we are in a different scenario.
They want to access this data from our authentication service.
Let's undo the changes we made to the manage component.
Next, let's open the authentication service.
We are going to replicate the same behavior.
We are going to get different results.
Let's see what happens at the top of the file.
We will import the activated route class.
Next, we will inject this service into the constructor function with the alias route.
Lastly, we will subscribe to the this route data observable in the constructor function for our subscription.
We will pass in the console log function.
Refresh the page in the browser.
I'm on the Manage page unlike before.
The object is entirely empty.
We aren't able to view data from our route, which is a huge problem.
Without this information, we cannot conditionally redirect the user a based on the data in the route.
It's completely possible to get the data, but it's going to require work before moving on.
Let's remove the subscription from the constructor function of our service.
We will not remove the activated route service from the authentication service.
We are going to need it later.
Let's work on the solution in the next lecture.