Created
May 6, 2020 16:32
-
-
Save adamgen/8687435a822aaef825162fbaf5296d1e to your computer and use it in GitHub Desktop.
NGRX | Use effects and router-store to isolate route related side π§πΌββοΈ effects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
export class MyComponent { | |
id: string; | |
id$ = this.activatedRoute.params.pipe( | |
map(params => params.id), | |
tap(id => this.id = id), | |
); | |
constructor( | |
private activatedRoute: ActivatedRoute, | |
) {} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setCurrentCourse$ = createEffect(() => this.actions$.pipe( | |
ofType(ROUTER_NAVIGATED), // get router navigated ngrx actions | |
mergeMap(() => this.store.pipe( | |
select(selectRouteParam(βidβ))) // get the id from the router store | |
), | |
map((id: string) => setIds({ id })), // dispatch a new action to set the selected id | |
)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Store, select } from β@ngrx/storeβ; | |
import { Component } from β@angular/coreβ; | |
@Component({ | |
selector: βapp-rootβ, | |
template: `{{selectedId$ | async | json}}`, | |
}) | |
export class HomeComponent { | |
selectedId$ = this.store.pipe(select((state: any) => state.featureName.selectedId)); | |
constructor( | |
private store: Store, | |
) { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment