Skip to content

Commit 092830e

Browse files
committed
docs: clean up effect for currentRoute; use toSignal instead
1 parent 8f336df commit 092830e

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

apps/kitchen-sink/src/app/app.component.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { Component, effect, inject, signal } from '@angular/core';
1+
import { Component, inject } from '@angular/core';
2+
import { toSignal } from '@angular/core/rxjs-interop';
23
import { NavigationEnd, Router, RouterOutlet } from '@angular/router';
3-
import { filter, map, tap } from 'rxjs';
4+
import { filter, map } from 'rxjs';
45

56
@Component({
67
selector: 'app-root',
@@ -17,26 +18,18 @@ import { filter, map, tap } from 'rxjs';
1718
imports: [RouterOutlet],
1819
})
1920
export class AppComponent {
20-
currentRoute = signal('soba');
21-
2221
private router = inject(Router);
2322

24-
constructor() {
25-
effect((onCleanup) => {
26-
const sub = this.router.events
27-
.pipe(
28-
filter((event) => event instanceof NavigationEnd),
29-
map(() => this.router.url),
30-
tap((url) => {
31-
const [segment] = url.split('/').filter(Boolean);
32-
this.currentRoute.set(segment);
33-
}),
34-
)
35-
.subscribe();
36-
37-
onCleanup(() => sub.unsubscribe());
38-
});
39-
}
23+
currentRoute = toSignal(
24+
this.router.events.pipe(
25+
filter((event) => event instanceof NavigationEnd),
26+
map(() => {
27+
const [segment] = this.router.url.split('/').filter(Boolean);
28+
return segment;
29+
}),
30+
),
31+
{ initialValue: 'soba' },
32+
);
4033

4134
onChange(event: Event) {
4235
const target = event.target as HTMLSelectElement;

0 commit comments

Comments
 (0)