diff --git a/packages/yew-router/src/lib.rs b/packages/yew-router/src/lib.rs index 1cf262e21d8..22abcf74c3b 100644 --- a/packages/yew-router/src/lib.rs +++ b/packages/yew-router/src/lib.rs @@ -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`. //! diff --git a/packages/yew-router/src/navigator.rs b/packages/yew-router/src/navigator.rs index 48345609f83..e348f47ae77 100644 --- a/packages/yew-router/src/navigator.rs +++ b/packages/yew-router/src/navigator.rs @@ -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(&self, route: &R, query: &Q) -> NavigationResult<()> + pub fn push_with_query(&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(&self, route: &R, query: &Q) -> NavigationResult<()> + pub fn replace_with_query(&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( &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( &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( diff --git a/website/docs/concepts/router.mdx b/website/docs/concepts/router.mdx index 75613394c12..dfc0e6bc779 100644 --- a/website/docs/concepts/router.mdx +++ b/website/docs/concepts/router.mdx @@ -350,14 +350,12 @@ fn create(ctx: &Context) -> 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