ngx-router
Version:
A utility to get route params, query params from Activated Route by using dependency injection
65 lines (45 loc) • 2.13 kB
Markdown
# ngx-router
[](https://github.com/TinkoffCreditSystems/angular-open-source-starter)


## What's ngx-router
`ngx-router` is a utility that allows Angular users to get route param, query param, or route data from `ActivatedRoute` by using dependency injection. It's fully tree-shakable. Supported Angular version >= 9.
More information in the [indepth article](https://indepth.dev/posts/1471/leveraging-dependency-injection-to-reduce-duplicated-code-in-angular)
## Usage
1. Install package
```shell
# install ngx-router
npm i ngx-router
```
2. Declare Injection Token to hold route param, query param, or route data
3. Use
- `routeParamFactory`, `routeParamSnapshotFactory` to get value from route param as an Observable or as a snapshot
- `queryParamFactory`, `queryParamSanpshotFactory` to get value from query param as an Observable or as a snapshot
- `routeDataFactory`, `routeDataSnapshotFactory` to get value from route `data` as an Observable or as a snapshot
4. Inject the token in step 2 and use it.
```javascript
// Suppose you have route config as following
export const appRoutes: Routes = [
{
path: ':someId',
component: SomeComponent,
}
]
import { routeParamFactory } from 'ngx-router/route-param';
import { ActivatedRoute } from '/router';
export const APP_SOME_ID = new InjectionToken<Observable<string>>('stream of :someId route param');
({
template: `<p>someId value: {{ someId$ | async }} </p>`,
selecttor: 'app-some-component',
providers: [
{
provide: APP_SOME_ID,
useFactory: routeParamFactory('someId'),
deps: [ActivatedRoute]
}
]
})
export class SomeComponent {
constructor( (APP_SOME_ID) public readonly someId$: Observable<string>) {}
}
```