|
1 |
| -import { Component } from '@angular/core'; |
2 |
| -import { RouterOutlet } from '@angular/router'; |
| 1 | +import { Component, effect, inject, signal } from '@angular/core'; |
| 2 | +import { NavigationEnd, Router, RouterOutlet } from '@angular/router'; |
| 3 | +import { filter, map, tap } from 'rxjs'; |
3 | 4 |
|
4 | 5 | @Component({
|
5 | 6 | selector: 'app-root',
|
6 | 7 | standalone: true,
|
7 | 8 | template: `
|
8 | 9 | <router-outlet />
|
| 10 | + <select class="absolute bottom-4 right-4 font-mono" [value]="currentRoute()" (change)="onChange($event)"> |
| 11 | + <option value="soba">/soba</option> |
| 12 | + <option value="cannon">/cannon</option> |
| 13 | + <option value="postprocessing">/postprocessing</option> |
| 14 | + </select> |
9 | 15 | `,
|
10 | 16 | imports: [RouterOutlet],
|
11 | 17 | })
|
12 |
| -export class AppComponent {} |
| 18 | +export class AppComponent { |
| 19 | + currentRoute = signal('soba'); |
| 20 | + |
| 21 | + private router = inject(Router); |
| 22 | + |
| 23 | + constructor() { |
| 24 | + effect((onCleanup) => { |
| 25 | + const sub = this.router.events |
| 26 | + .pipe( |
| 27 | + filter((event) => event instanceof NavigationEnd), |
| 28 | + map(() => this.router.url), |
| 29 | + tap((url) => { |
| 30 | + const [segment] = url.split('/').filter(Boolean); |
| 31 | + this.currentRoute.set(segment); |
| 32 | + }), |
| 33 | + ) |
| 34 | + .subscribe(); |
| 35 | + |
| 36 | + onCleanup(() => sub.unsubscribe()); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + onChange(event: Event) { |
| 41 | + const target = event.target as HTMLSelectElement; |
| 42 | + void this.router.navigate([target.value]); |
| 43 | + } |
| 44 | +} |
0 commit comments