Skip to content

Commit cb82f40

Browse files
committed
docs: improve docs for withComponentInputBinding
Add mention about inputs potentially becoming `undefined`.
1 parent c11527f commit cb82f40

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

adev/src/content/guide/routing/common-router-tasks.md

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ To edit an item, users click an Edit button, which opens an `EditGroceryItem` co
1111
You want that component to retrieve the `id` for the grocery item so it can display the right information to the user.
1212

1313
Use a route to pass this type of information to your application components.
14-
To do so, you use the [withComponentInputBinding](api/router/withComponentInputBinding) feature with `provideRouter` or the `bindToComponentInputs` option of `RouterModule.forRoot`.
14+
To do so, you use the [`withComponentInputBinding`](api/router/withComponentInputBinding) feature with `provideRouter` or the `bindToComponentInputs` option of `RouterModule.forRoot`.
1515

1616
To get information from a route:
1717

@@ -29,25 +29,44 @@ providers: [
2929

3030
</docs-step>
3131

32-
<docs-step title="Add an `Input` to the component">
32+
<docs-step title="Add an `input` to the component">
3333

34-
Update the component to have an `Input` matching the name of the parameter.
34+
Update the component to have an `input` property matching the name of the parameter.
3535

3636
```ts
37-
@Input()
38-
set id(heroId: string) {
39-
this.hero$ = this.service.getHero(heroId);
40-
}
37+
id = input<string|undefined>();
38+
effect(() => {
39+
const heroId = this.id();
40+
if(heroId) {
41+
hero = this.service.getHero(heroId)
42+
}
43+
});
4144
```
4245

43-
NOTE: You can bind all route data with key, value pairs to component inputs: static or resolved route data, path parameters, matrix parameters, and query parameters.
44-
If you want to use the parent components route info you will need to set the router `paramsInheritanceStrategy` option:
45-
`withRouterConfig({paramsInheritanceStrategy: 'always'})`
46-
4746
</docs-step>
47+
<docs-step title="Optional: Use a default value">
48+
Binded inputs can be set to `undefined` when no route elements match.
49+
In this situation, the router will overwrite the default value of the signal input.<br>
50+
It is recommended that such input reflect that by including `undefined` in the typing.
51+
52+
You can provide a default value by either setting the `transform` option on the `input` or rely on a local state with `linkedSignal`.
53+
54+
```ts
55+
id = input.required({
56+
transform: (maybeUndefined: string | undefined) => maybeUndefined ?? '0',
57+
});
58+
// or
59+
id = input<string|undefined>();
60+
internalId = linkedSignal(() => this.id() ?? getDefaultId());
61+
```
4862

63+
</docs-step>
4964
</docs-workflow>
5065

66+
NOTE: You can bind all route data with key, value pairs to component inputs: static or resolved route data, path parameters, matrix parameters, and query parameters.
67+
If you want to use the parent components route info you will need to set the router `paramsInheritanceStrategy` option:
68+
`withRouterConfig({paramsInheritanceStrategy: 'always'})`
69+
5170
## Displaying a 404 page
5271

5372
To display a 404 page, set up a [wildcard route](guide/routing/common-router-tasks#setting-up-wildcard-routes) with the `component` property set to the component you'd like to use for your 404 page as follows:

0 commit comments

Comments
 (0)