Skip to content

Commit

Permalink
feat(router): add activated route provider (#2988)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Aug 15, 2024
1 parent 86b0973 commit 3b6103c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
26 changes: 26 additions & 0 deletions libs/router/src/activated-route/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {
APP_INITIALIZER,
DestroyRef,
inject,
Provider,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';

import { DaffRouterActivatedRoute } from './service';

/**
* Provides the initializer for {@link DaffRouterActivatedRoute}.
*/
export const provideDaffRouterActivatedRoute = (): Provider => ({
provide: APP_INITIALIZER,
multi: true,
useFactory: () => {
const service = inject(DaffRouterActivatedRoute);
const destroyRef = inject(DestroyRef);
return () => {
service.route$.pipe(
takeUntilDestroyed(destroyRef),
).subscribe();
};
},
});
1 change: 1 addition & 0 deletions libs/router/src/activated-route/public_api.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './service';
export * from './provider';
1 change: 1 addition & 0 deletions libs/router/src/activated-route/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const getActivatedRoute = (routerState: RouterState): ActivatedRoute => {
* Note that this service operates by listening to router events. It is therefore recommended to
* inject this service in the root and subscribe to `route$` on app init so that all routing events are captured.
* The consumer can then subscribe at any later time (after all navigations) and the emission stream will be replayed.
* {@link provideDaffRouterActivatedRoute} is the recommended way to do this.
*/
@Injectable({
providedIn: 'root',
Expand Down

0 comments on commit 3b6103c

Please sign in to comment.