Skip to content

Commit df543d0

Browse files
feat: add functions to expose router injectables through inject() API (#93)
1 parent bbdbb61 commit df543d0

File tree

9 files changed

+69
-37
lines changed

9 files changed

+69
-37
lines changed

apps/example-app/src/app/auth/effects/auth.effects.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Injectable } from '@angular/core';
22
import { MatDialog } from '@angular/material/dialog';
3-
import { Router } from '@angular-component/router';
3+
import { getRouter } from '@angular-component/router';
44
import { Actions, ofType, createEffect } from '@ngrx/effects';
55
import { of } from 'rxjs';
66
import { catchError, exhaustMap, map, tap } from 'rxjs/operators';
@@ -16,6 +16,8 @@ import { UserActions } from '@example-app/core/actions';
1616

1717
@Injectable()
1818
export class AuthEffects {
19+
private router = getRouter();
20+
1921
login$ = createEffect(() =>
2022
this.actions$.pipe(
2123
ofType(LoginPageActions.login),
@@ -77,7 +79,6 @@ export class AuthEffects {
7779
constructor(
7880
private actions$: Actions,
7981
private authService: AuthService,
80-
private router: Router,
8182
private dialog: MatDialog
8283
) {}
8384
}

apps/example-app/src/app/books/containers/view-book-page.component.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, OnDestroy, ChangeDetectionStrategy } from '@angular/core';
22
import { Store } from '@ngrx/store';
3-
import { RouteParams } from '@angular-component/router';
3+
import { getRouteParams } from '@angular-component/router';
44
import { Subscription } from 'rxjs';
55
import { map } from 'rxjs/operators';
66

@@ -24,12 +24,10 @@ import { ViewBookPageActions } from '@example-app/books/actions';
2424
})
2525
export class ViewBookPageComponent implements OnDestroy {
2626
actionsSubscription: Subscription;
27+
private routeParams$ = getRouteParams<{ id: string }>();
2728

28-
constructor(
29-
store: Store<fromBooks.State>,
30-
routeParams$: RouteParams<{ id: string }>
31-
) {
32-
this.actionsSubscription = routeParams$
29+
constructor(store: Store<fromBooks.State>) {
30+
this.actionsSubscription = this.routeParams$
3331
.pipe(map((params) => ViewBookPageActions.selectBook({ id: params.id })))
3432
.subscribe((action) => store.dispatch(action));
3533
}

apps/example-app/src/app/core/containers/not-found-page.component.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, ChangeDetectionStrategy } from '@angular/core';
2-
import { Router } from '@angular-component/router';
2+
import { getRouter } from '@angular-component/router';
33

44
@Component({
55
selector: 'bc-not-found-page',
@@ -26,7 +26,7 @@ import { Router } from '@angular-component/router';
2626
],
2727
})
2828
export class NotFoundPageComponent {
29-
constructor(private router: Router) {}
29+
private router = getRouter();
3030

3131
goHome() {
3232
this.router.go('/');

libs/router/src/lib/link-active.directive.ts

+8-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
import { LinkTo } from './link-to.directive';
1515
import { Router } from './router.service';
1616
import { combineLatest, of, Subject, Subscription } from 'rxjs';
17-
import { map, mapTo, startWith, takeUntil } from 'rxjs/operators';
17+
import { map, startWith, takeUntil } from 'rxjs/operators';
1818
import { filterNullable } from './operators/filter-nullable.operator';
1919

2020
export interface LinkActiveOptions {
@@ -78,21 +78,19 @@ export class LinkActive implements AfterContentInit, OnDestroy, OnChanges {
7878
}
7979

8080
const contentLinks$ = this.links
81-
? this.links
82-
.toArray()
83-
.map((link) =>
84-
link.hrefUpdated.pipe(
85-
startWith(link.linkHref),
86-
mapTo(link.linkHref),
87-
filterNullable()
88-
)
81+
? this.links.toArray().map((link) =>
82+
link.hrefUpdated.pipe(
83+
startWith(link.linkHref),
84+
map(() => link.linkHref),
85+
filterNullable()
8986
)
87+
)
9088
: [];
9189

9290
const link$ = this.link
9391
? this.link.hrefUpdated.pipe(
9492
startWith(this.link.linkHref),
95-
mapTo(this.link.linkHref),
93+
map(() => this.link.linkHref),
9694
filterNullable()
9795
)
9896
: of('');
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Directive, Input } from '@angular/core';
1+
import { Directive, Input, Type } from '@angular/core';
22

33
@Directive({
44
selector: '[routeComponent]',
55
standalone: true,
66
})
77
export class RouteComponentTemplate {
8-
@Input() routeComponent: any;
8+
@Input() routeComponent: Type<any>;
99
}

libs/router/src/lib/route-params.service.ts

+28
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { inject } from '@angular/core';
12
import { Observable } from 'rxjs';
23

34
export interface Params {
@@ -15,3 +16,30 @@ export function compareParams(previous: Params, current: Params): boolean {
1516
previous === current || JSON.stringify(previous) === JSON.stringify(current)
1617
);
1718
}
19+
20+
/**
21+
* Returns the RoutePath observable from the current injector
22+
*
23+
* @returns RoutePath
24+
*/
25+
export function getRoutePath<T extends string = string>() {
26+
return inject<RoutePath<T>>(RoutePath);
27+
}
28+
29+
/**
30+
* Returns the RoutePath observable from the current injector
31+
*
32+
* @returns RouteParams
33+
*/
34+
export function getRouteParams<T extends Params = Params>() {
35+
return inject<RouteParams<T>>(RouteParams);
36+
}
37+
38+
/**
39+
* Returns the QueryParams observable from the current injector
40+
*
41+
* @returns QueryParams
42+
*/
43+
export function getQueryParams<T extends Params = Params>() {
44+
return inject<QueryParams<T>>(QueryParams);
45+
}

libs/router/src/lib/route.component.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ import { Params, RouteParams, RoutePath } from './route-params.service';
2929
import { RouterComponent } from './router.component';
3030
import { Router } from './router.service';
3131

32-
export function getRouteParams(routeComponent: RouteComponent) {
33-
return routeComponent.routeParams$;
34-
}
35-
36-
export function getRoutePath(routeComponent: RouteComponent) {
37-
return routeComponent.routePath$;
38-
}
39-
4032
interface State {
4133
params: Params;
4234
path: string;
@@ -59,12 +51,16 @@ interface State {
5951
providers: [
6052
{
6153
provide: RouteParams,
62-
useFactory: getRouteParams,
54+
useFactory(routeComponent: RouteComponent) {
55+
return routeComponent.routeParams$;
56+
},
6357
deps: [[new Self(), RouteComponent]],
6458
},
6559
{
6660
provide: RoutePath,
67-
useFactory: getRoutePath,
61+
useFactory(routeComponent: RouteComponent) {
62+
return routeComponent.routePath$;
63+
},
6864
deps: [[new Self(), RouteComponent]],
6965
},
7066
],

libs/router/src/lib/router.module.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ export const components = [
2222
RouteComponentTemplate,
2323
];
2424

25-
export function getQueryParams(router: Router) {
26-
return router.queryParams$;
27-
}
28-
2925
export function provideComponentRouter() {
3026
return [
3127
UrlParser,
3228
{ provide: LocationStrategy, useClass: PathLocationStrategy },
33-
{ provide: QueryParams, deps: [Router], useFactory: getQueryParams },
29+
{
30+
provide: QueryParams,
31+
deps: [Router],
32+
useFactory(router: Router) {
33+
return router.queryParams$;
34+
},
35+
},
3436
];
3537
}
3638

libs/router/src/lib/router.service.ts

+9
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,12 @@ export class Router {
124124
return new URLSearchParams(params).toString();
125125
}
126126
}
127+
128+
/**
129+
* Returns the Router instance from the current injector
130+
*
131+
* @returns Router
132+
*/
133+
export function getRouter() {
134+
return inject(Router);
135+
}

0 commit comments

Comments
 (0)