Skip to content

Commit 3b6103c

Browse files
authored
feat(router): add activated route provider (#2988)
1 parent 86b0973 commit 3b6103c

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {
2+
APP_INITIALIZER,
3+
DestroyRef,
4+
inject,
5+
Provider,
6+
} from '@angular/core';
7+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
8+
9+
import { DaffRouterActivatedRoute } from './service';
10+
11+
/**
12+
* Provides the initializer for {@link DaffRouterActivatedRoute}.
13+
*/
14+
export const provideDaffRouterActivatedRoute = (): Provider => ({
15+
provide: APP_INITIALIZER,
16+
multi: true,
17+
useFactory: () => {
18+
const service = inject(DaffRouterActivatedRoute);
19+
const destroyRef = inject(DestroyRef);
20+
return () => {
21+
service.route$.pipe(
22+
takeUntilDestroyed(destroyRef),
23+
).subscribe();
24+
};
25+
},
26+
});
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './service';
2+
export * from './provider';

libs/router/src/activated-route/service.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const getActivatedRoute = (routerState: RouterState): ActivatedRoute => {
2727
* Note that this service operates by listening to router events. It is therefore recommended to
2828
* inject this service in the root and subscribe to `route$` on app init so that all routing events are captured.
2929
* The consumer can then subscribe at any later time (after all navigations) and the emission stream will be replayed.
30+
* {@link provideDaffRouterActivatedRoute} is the recommended way to do this.
3031
*/
3132
@Injectable({
3233
providedIn: 'root',

0 commit comments

Comments
 (0)