-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
112 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,78 @@ | ||
# @ngworker/router-component-store | ||
|
||
Angular Router-connecting NgRx component stores. | ||
|
||
## Compatibility | ||
|
||
Required peer dependencies: | ||
|
||
- Angular >=12.2 | ||
- NgRx Component Store >=12.0 | ||
- RxJS >=7.0 | ||
|
||
Published with partial Ivy compilation. | ||
|
||
## API | ||
|
||
Two router stores are available and implement the same public API: | ||
|
||
| API | Description | | ||
| ----------------------------------------------------------- | ------------------------------------------ | | ||
| currentRoute$: Observable<MinimalActivatedRouteSnapshot> | Select the current route. | | ||
| fragment$: Observable<string \| null> | Select the current route fragment. | | ||
| queryParams$: Observable<Params> | Select the current route query parameters. | | ||
| routeData$: Observable<Data> | Select the current route data. | | ||
| routeParams$: Observable<Params> | Select the current route parameters. | | ||
| url$: Observable<string> | Select the current URL. | | ||
| selectQueryParam<TValue>(param: string): Observable<TValue> | Select the specified query parameter. | | ||
| selectRouteParam<TValue>(param: string): Observable<TValue> | Select the specified route paramter. | | ||
|
||
The `GlobalRouterStore` is never destroyed but can be injected in any class. | ||
|
||
The `LocalRouterStore` requires a component-level provider, follows the | ||
lifecycle of that component, and can be injected in declarables as well as | ||
other component-level services. | ||
|
||
### GlobalRouterStore | ||
|
||
An application-wide router store. Can be injected in any class. Implicitly | ||
provided in the root module injector. | ||
|
||
Usage: | ||
|
||
```ts | ||
// (...) | ||
import { GlobalRouterStore } from '@ngworker/router-component-store'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class HeroService { | ||
activeHeroId$: Observable<number> = this.routerStore.selectQueryParam('id'); | ||
|
||
constructor(private routerStore: GlobalRouterStore) {} | ||
} | ||
``` | ||
|
||
### LocalRouterStore | ||
|
||
A component-level router store. Can be injected in any directive, component, | ||
pipe, or component-level service. Explicitly provided in a component sub-tree | ||
using `Component.providers` or `Component.viewProviders`. | ||
|
||
Usage: | ||
|
||
```ts | ||
// (...) | ||
import { LocalRouterStore } from '@ngworker/router-component-store'; | ||
|
||
@Component({ | ||
// (...) | ||
providers: [LocalRouterStore], | ||
}) | ||
export class HeroDetailComponent { | ||
heroId$: Observable<number> = this.routerStore.selectQueryParam('id'); | ||
|
||
constructor(private routerStore: LocalRouterStore) {} | ||
} | ||
``` |
This file contains 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
This file contains 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
15 changes: 15 additions & 0 deletions
15
packages/router-component-store/src/lib/router-component-store.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Data, Params } from '@angular/router'; | ||
import { Observable } from 'rxjs'; | ||
|
||
import { MinimalActivatedRouteSnapshot } from './@ngrx/router-store/minimal_serializer'; | ||
|
||
export abstract class RouterComponentStore { | ||
abstract readonly currentRoute$: Observable<MinimalActivatedRouteSnapshot>; | ||
abstract readonly fragment$: Observable<string | null>; | ||
abstract readonly queryParams$: Observable<Params>; | ||
abstract readonly routeData$: Observable<Data>; | ||
abstract readonly routeParams$: Observable<Params>; | ||
abstract readonly url$: Observable<string>; | ||
abstract selectQueryParam<TValue>(param: string): Observable<TValue>; | ||
abstract selectRouteParam<TValue>(param: string): Observable<TValue>; | ||
} |