d48cda4081
CI / Test (ubuntu-latest, Node 18.x, bun) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, npm) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, pnpm) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 18.x, yarn) (push) Failing after 15m1s
CI / Test (ubuntu-latest, Node 20.x, bun) (push) Failing after 17m13s
CI / Test (ubuntu-latest, Node 20.x, npm) (push) Failing after 18m42s
CI / Test (ubuntu-latest, Node 20.x, pnpm) (push) Failing after 15m0s
CI / Test (ubuntu-latest, Node 20.x, yarn) (push) Failing after 49m44s
CI / Test (ubuntu-latest, Node 22.x, bun) (push) Failing after 51m55s
CI / Test (ubuntu-latest, Node 22.x, pnpm) (push) Failing after 21m57s
CI / Test (ubuntu-latest, Node 22.x, npm) (push) Failing after 37m39s
CI / Test (ubuntu-latest, Node 22.x, yarn) (push) Failing after 34m7s
CI / Validate Components (push) Failing after 37m15s
CI / Python Tests (push) Failing after 10m1s
CI / Security Scan (push) Failing after 10m1s
CI / Lint (push) Failing after 17m12s
CI / Coverage (push) Failing after 20m19s
CI / Test (macos-latest, Node 18.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 18.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 18.x, yarn) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 20.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 20.x, yarn) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, bun) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, npm) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, pnpm) (push) Has been cancelled
CI / Test (macos-latest, Node 22.x, yarn) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, npm) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, pnpm) (push) Has been cancelled
CI / Test (windows-latest, Node 22.x, yarn) (push) Has been cancelled
70 lines
1.5 KiB
Markdown
70 lines
1.5 KiB
Markdown
# Data Resolvers
|
|
|
|
Data resolvers fetch data before a route activates, ensuring components have the necessary data upon rendering.
|
|
|
|
## Creating a Resolver
|
|
|
|
Implement the `ResolveFn` type.
|
|
|
|
```ts
|
|
export const userResolver: ResolveFn<User> = (route, state) => {
|
|
const userService = inject(UserService);
|
|
const id = route.paramMap.get('id')!;
|
|
return userService.getUser(id);
|
|
};
|
|
```
|
|
|
|
## Configuring the Route
|
|
|
|
Add the resolver under the `resolve` key.
|
|
|
|
```ts
|
|
{
|
|
path: 'user/:id',
|
|
component: UserProfile,
|
|
resolve: {
|
|
user: userResolver
|
|
}
|
|
}
|
|
```
|
|
|
|
## Accessing Resolved Data
|
|
|
|
### 1. Via `ActivatedRoute` (Traditional)
|
|
|
|
```ts
|
|
private route = inject(ActivatedRoute);
|
|
data = toSignal(this.route.data);
|
|
user = computed(() => this.data().user);
|
|
```
|
|
|
|
### 2. Via Component Inputs (Modern)
|
|
|
|
Enable `withComponentInputBinding()` in `provideRouter` to pass resolved data directly to `@Input` or `input()`.
|
|
|
|
```ts
|
|
// app.config.ts
|
|
provideRouter(routes, withComponentInputBinding());
|
|
|
|
// component.ts
|
|
user = input.required<User>();
|
|
```
|
|
|
|
## Error Handling
|
|
|
|
Navigation is blocked if a resolver fails.
|
|
|
|
- Use `withNavigationErrorHandler` for global handling.
|
|
- Use `catchError` within the resolver to return a `RedirectCommand` or fallback data.
|
|
|
|
```ts
|
|
return userService
|
|
.get(id)
|
|
.pipe(catchError(() => of(new RedirectCommand(router.parseUrl('/error')))));
|
|
```
|
|
|
|
## Best Practices
|
|
|
|
- **Keep it lightweight**: Fetch only critical data.
|
|
- **Provide feedback**: Listen to router events to show a global loading bar during navigation, as the UI stays on the old page until the resolver finishes.
|