Skip to content

Commit 1199776

Browse files
committed
feat: add navigation effects
1 parent 4d5d839 commit 1199776

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

packages/router-component-store/src/lib/router-history-store/router-history.store.spec.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AsyncPipe, Location, NgIf } from '@angular/common';
1+
import { AsyncPipe, NgIf } from '@angular/common';
22
import { Component, inject, NgZone } from '@angular/core';
33
import { TestBed } from '@angular/core/testing';
44
import { By } from '@angular/platform-browser';
@@ -39,13 +39,11 @@ function createTestComponent(name: string, selector: string) {
3939
`,
4040
})
4141
class TestAppComponent {
42-
#location = inject(Location);
43-
4442
protected routerHistory = inject(RouterHistoryStore);
4543

4644
onBack(event: MouseEvent) {
4745
event.preventDefault();
48-
this.#location.back();
46+
this.routerHistory.onNavigateBack();
4947
}
5048
}
5149

packages/router-component-store/src/lib/router-history-store/router-history.store.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Location as NgLocation } from '@angular/common';
12
import {
23
APP_INITIALIZER,
34
FactoryProvider,
@@ -14,7 +15,7 @@ import {
1415
Router,
1516
} from '@angular/router';
1617
import { ComponentStore, provideComponentStore } from '@ngrx/component-store';
17-
import { filter, map, Observable, switchMap, take } from 'rxjs';
18+
import { filter, map, Observable, pipe, switchMap, take, tap } from 'rxjs';
1819
import { filterRouterEvents } from '../filter-router-event.operator';
1920
import { isPopstateNavigationStart } from './popstate-navigation-start';
2021
import {
@@ -57,6 +58,7 @@ export function provideRouterHistoryStore(): Provider[] {
5758

5859
@Injectable()
5960
export class RouterHistoryStore extends ComponentStore<RouterHistoryState> {
61+
#location = inject(NgLocation);
6062
#router = inject(Router);
6163

6264
/**
@@ -163,6 +165,24 @@ export class RouterHistoryStore extends ComponentStore<RouterHistoryState> {
163165
this.#addRouterNavigatedSequence(this.#routerNavigated$);
164166
}
165167

168+
/**
169+
* Navigate back in the browser history.
170+
*
171+
* @remarks
172+
* This is only available when the browser history contains a back entry.
173+
*/
174+
onNavigateBack = this.effect<void>(pipe(tap(() => this.#location.back())));
175+
176+
/**
177+
* Navigate forward in the browser history.
178+
*
179+
* @remarks
180+
* This is only available when the browser history contains a forward entry.
181+
*/
182+
onNavigateForward = this.effect<void>(
183+
pipe(tap(() => this.#location.forward()))
184+
);
185+
166186
/**
167187
* Add a router navigated sequence to the router navigated history.
168188
*/

0 commit comments

Comments
 (0)