Using Selectors page
Learn how to use NgRx Selectors in a Component to access data from a store.
Quick Start: You can checkout this branch to get your codebase ready to work on this section.
Overview
Update
DashboardComponent
’susername$
anduserId$
members to use Login Selectors.Update
AuthenticationGuard
’scanActivate()
method to use Login Selector.
Problem 1: Update username$
and userId$
to use Login Selectors on DashboardComponent
DashboardComponent
should use the LoginSelectors.selectUsername
and LoginSelectors.selectUserId
Selectors for its username$
and userId$
members.
P1: What you need to know
Now that we have our Selectors defined, we can inject Store
into our Components
and use the select()
method to obtain slices of state using our Selectors:
In the DashboardComponent
, there is are TODO
’s where the Login Selectors should be used.
P1: Solution
src/app/dashboard/dashboard.component.ts
Problem 2: Update canActivate()
to use Login Selector on AuthenticationGuard
AuthenticationGuard
’s canActivate()
method should use the LoginSelectors.selectToken
Selector.
P2: What you need to know
In the AuthenticationGuard
, there is a TODO
where the Login Selector should be used.
P2: Solution
src/app/guards/authentication.guard.ts
Wrap-up: By the end of this section, your code should match this branch. You can also compare the code changes for our solution to this section on GitHub or you can use the following command in your terminal: