You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/concepts/router.md
+59-67Lines changed: 59 additions & 67 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,87 +5,79 @@ description: Yew's official router
5
5
6
6
[The router on crates.io](https://crates.io/crates/yew-router)
7
7
8
-
Routers in Single Page Applications \(SPA\) handle displaying different pages depending on what the URL is. Instead of the default behavior of requesting a different remote resource when a link is clicked, the router instead sets the URL locally to point to a valid route in your application. The router then detects this change and then decides what to render.
8
+
Routers in Single Page Applications (SPA) handle displaying different pages depending on what the URL is.
9
+
Instead of the default behavior of requesting a different remote resource when a link is clicked,
10
+
the router instead sets the URL locally to point to a valid route in your application.
11
+
The router then detects this change and then decides what to render.
9
12
10
-
## Core Elements
13
+
## Usage
11
14
12
-
### `Route`
13
-
14
-
Contains a String representing everything after the domain in the url and optionally the state stored in the history API.
15
-
16
-
### `RouteService`
17
-
18
-
Communicates with the browser to get and set Routes.
19
-
20
-
### `RouteAgent`
21
-
22
-
Owns a RouteService and is used to coordinate updates when the route changes, either from within the application logic or from an event fired from the browser.
23
-
24
-
### `Switch`
25
-
26
-
The `Switch` trait is used to convert a `Route` to and from the implementer of this trait.
27
-
28
-
### `Router`
29
-
30
-
The Router component communicates with `RouteAgent` and will automatically resolve Routes it gets from the agent into switches, which it will expose via a `render` prop that allows specifying how the resulting switch gets converted to `Html`.
31
-
32
-
## How to use the router
33
-
34
-
First, you want to create a type that represents all the states of your application. Do note that while this typically is an enum, structs are supported as well, and that you can nest other items that implement `Switch` inside.
35
-
36
-
Then you should derive `Switch` for your type. For enums, every variant must be annotated with `
37
-
#[at = "/some/route"]` (or `#[at = "/some/route"]`, but this is being phased out in favor of "at"),
38
-
or if you use a struct instead, that must appear outside the struct declaration.
15
+
The Router component. It takes in a callback and renders the HTML based on the returned value of the callback. It is usually placed
16
+
at the top of the application.
39
17
18
+
Routes are defined by an `enum` which derives `Routable`:
40
19
```rust
41
-
#[derive(Switch)]
42
-
enumAppRoute {
43
-
#[at ="/login"]
44
-
Login,
45
-
#[at ="/register"]
46
-
Register,
47
-
#[at ="/delete_account"]
48
-
Delete,
49
-
#[at ="/posts/{id}"]
50
-
ViewPost(i32),
51
-
#[at ="/posts/view"]
52
-
ViewPosts,
53
-
#[at ="/"]
54
-
Home
20
+
#[derive(Routable)]
21
+
enumRoute {
22
+
#[at("/")]
23
+
Home,
24
+
#[at("/secure")]
25
+
Secure,
26
+
#[not_found]
27
+
#[at("/404")]
28
+
NotFound,
55
29
}
56
30
```
57
31
58
-
:::caution
59
-
Do note that the implementation generated by the derive macro for `Switch` will try to match each
60
-
variant in order from first to last, so if any route could possibly match two of your specified
61
-
`to` annotations, then the first one will match, and the second will never be tried. For example,
62
-
if you defined the following `Switch`, the only route that would be matched would be
63
-
`AppRoute::Home`.
32
+
The `Router` component takes the `Routable` enum as its type parameter, finds the first variant whose path matches the
33
+
browser's current URL and passes it to the `render` callback. The callback then decides what to render.
34
+
In case no path is matched, the router navigates to the path with `not_found` attribute. If no route is specified,
35
+
nothing is rendered, and a message is logged to console stating that no route was matched.
36
+
37
+
`yew_router::current_route` is used to programmatically obtain the current route.
38
+
`yew_router::attach_route_listener` is used to attach a listener which is called every time route is changed.
You can also capture sections using variations of `{}` within your `#[at = ""]` annotation. `{}` means capture text until the next separator \(either "/", "?", "&", or "\#" depending on the context\). `{*}` means capture text until the following characters match, or if no characters are present, it will match anything. `{<number>}` means capture text until the specified number of separators are encountered \(example: `{2}` will capture until two separators are encountered\).
65
+
### Navigation
66
+
67
+
To navigate between pages, use either a `Link` component (which renders a `<a>` element) or the `yew_router::push_route` function.
68
+
69
+
### Query Parameters
70
+
71
+
#### Specifying query parameters when navigating
72
+
73
+
In order to specify query parameters when navigating to a new route, use `yew_router::push_route_with_query` function.
74
+
It uses `serde` to serialize the parameters into query string for the URL so any type that implements `Serialize` can be passed.
75
+
In its simplest form this is just a `HashMap` containing string pairs.
85
76
86
-
For structs and enums with named fields, you must specify the field's name within the capture group like so: `{user_name}` or `{*:age}`.
77
+
#### Obtaining query parameters for current route
87
78
88
-
The Switch trait works with capture groups that are more structured than just Strings. You can specify any type that implements `Switch`. So you can specify that the capture group is a `usize`, and if the captured section of the URL can't be converted to it, then the variant won't match.
79
+
`yew_router::parse_query` is used to obtain the query parameters.
80
+
It uses `serde` to deserialize the parameters from query string in the URL.
0 commit comments