Introduces the FromQuery and IntoQuery traits (#3565)

* Introduces the FromQuery and IntoQuery traits

* Added documentation
This commit is contained in:
Robert Schütte 2025-04-30 09:46:41 +02:00 committed by GitHub
parent 3b5c8910eb
commit 718cd29eea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 17 deletions

View File

@ -90,6 +90,12 @@ pub mod history {
};
}
pub mod query {
//! A module that provides custom query serialization & deserialization.
pub use gloo::history::query::{FromQuery, Raw, ToQuery};
}
pub mod prelude {
//! Prelude module to be imported when working with `yew-router`.
//!

View File

@ -1,8 +1,7 @@
use std::borrow::Cow;
use serde::Serialize;
use crate::history::{AnyHistory, History, HistoryError, HistoryResult};
use crate::query::ToQuery;
use crate::routable::Routable;
pub type NavigationError = HistoryError;
@ -93,20 +92,20 @@ impl Navigator {
}
/// Same as `.push()` but affix the queries to the end of the route.
pub fn push_with_query<R, Q>(&self, route: &R, query: &Q) -> NavigationResult<()>
pub fn push_with_query<R, Q>(&self, route: &R, query: Q) -> Result<(), Q::Error>
where
R: Routable,
Q: Serialize,
Q: ToQuery,
{
self.inner
.push_with_query(self.prefix_basename(&route.to_path()), query)
}
/// Same as `.replace()` but affix the queries to the end of the route.
pub fn replace_with_query<R, Q>(&self, route: &R, query: &Q) -> NavigationResult<()>
pub fn replace_with_query<R, Q>(&self, route: &R, query: Q) -> Result<(), Q::Error>
where
R: Routable,
Q: Serialize,
Q: ToQuery,
{
self.inner
.replace_with_query(self.prefix_basename(&route.to_path()), query)
@ -116,12 +115,12 @@ impl Navigator {
pub fn push_with_query_and_state<R, Q, T>(
&self,
route: &R,
query: &Q,
query: Q,
state: T,
) -> NavigationResult<()>
) -> Result<(), Q::Error>
where
R: Routable,
Q: Serialize,
Q: ToQuery,
T: 'static,
{
self.inner
@ -132,12 +131,12 @@ impl Navigator {
pub fn replace_with_query_and_state<R, Q, T>(
&self,
route: &R,
query: &Q,
query: Q,
state: T,
) -> NavigationResult<()>
) -> Result<(), Q::Error>
where
R: Routable,
Q: Serialize,
Q: ToQuery,
T: 'static,
{
self.inner.replace_with_query_and_state(

View File

@ -348,14 +348,12 @@ fn create(ctx: &Context<Self>) -> Self {
#### Specifying query parameters when navigating
In order to specify query parameters when navigating to a new route, use either `navigator.push_with_query` or
the `navigator.replace_with_query` functions. It uses `serde` to serialize the parameters into a query string for the URL so
any type that implements `Serialize` can be passed. In its simplest form, this is just a `HashMap` containing string
pairs.
the `navigator.replace_with_query` functions. It uses the `ToQuery` trait to serialize the parameters into a query string for the URL. The `ToQuery` trait is automatically implemented for `serde` so any type that implements `Serialize` can be passed. In its simplest form, this is just a `HashMap` containing string pairs. In more complex scenarios the `ToQuery` trait can be implemented manually for a custom query format.
#### Obtaining query parameters for the current route
`location.query` is used to obtain the query parameters. It uses `serde` to deserialize the parameters from the query string
in the URL.
`location.query` is used to obtain the query parameters. It uses the `FromQuery` trait to deserialize the parameters from the query string
in the URL. The `FromQuery` trait is automatically implemented for `serde` so any type that implements `Deserialize` can be passed. If the URL is formatted in an custom way, a manual implementation of `FromQuery` can be used.
## Nested Router