|
@@ -14,29 +14,35 @@ use dioxus_router::prelude::*;
|
|
#[derive(Routable, Clone)]
|
|
#[derive(Routable, Clone)]
|
|
#[rustfmt::skip]
|
|
#[rustfmt::skip]
|
|
enum Route {
|
|
enum Route {
|
|
- // segments that start with ?: are query segments
|
|
|
|
- #[route("/blog?:query_params")]
|
|
|
|
|
|
+ // segments that start with ?:.. are query segments that capture the entire query
|
|
|
|
+ #[route("/blog?:..query_params")]
|
|
BlogPost {
|
|
BlogPost {
|
|
// You must include query segments in child variants
|
|
// You must include query segments in child variants
|
|
- query_params: BlogQuerySegments,
|
|
|
|
|
|
+ query_params: ManualBlogQuerySegments,
|
|
|
|
+ },
|
|
|
|
+ // segments that follow the ?:field&:other_field syntax are query segments that follow the standard url query syntax
|
|
|
|
+ #[route("/autoblog?:name&:surname")]
|
|
|
|
+ AutomaticBlogPost {
|
|
|
|
+ name: String,
|
|
|
|
+ surname: String,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
-struct BlogQuerySegments {
|
|
|
|
|
|
+struct ManualBlogQuerySegments {
|
|
name: String,
|
|
name: String,
|
|
surname: String,
|
|
surname: String,
|
|
}
|
|
}
|
|
|
|
|
|
/// The display impl needs to display the query in a way that can be parsed:
|
|
/// The display impl needs to display the query in a way that can be parsed:
|
|
-impl Display for BlogQuerySegments {
|
|
|
|
|
|
+impl Display for ManualBlogQuerySegments {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "name={}&surname={}", self.name, self.surname)
|
|
write!(f, "name={}&surname={}", self.name, self.surname)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/// The query segment is anything that implements <https://docs.rs/dioxus-router/latest/dioxus_router/routable/trait.FromQuery.html>. You can implement that trait for a struct if you want to parse multiple query parameters.
|
|
/// The query segment is anything that implements <https://docs.rs/dioxus-router/latest/dioxus_router/routable/trait.FromQuery.html>. You can implement that trait for a struct if you want to parse multiple query parameters.
|
|
-impl FromQuery for BlogQuerySegments {
|
|
|
|
|
|
+impl FromQuery for ManualBlogQuerySegments {
|
|
fn from_query(query: &str) -> Self {
|
|
fn from_query(query: &str) -> Self {
|
|
let mut name = None;
|
|
let mut name = None;
|
|
let mut surname = None;
|
|
let mut surname = None;
|
|
@@ -57,13 +63,21 @@ impl FromQuery for BlogQuerySegments {
|
|
}
|
|
}
|
|
|
|
|
|
#[component]
|
|
#[component]
|
|
-fn BlogPost(cx: Scope, query_params: BlogQuerySegments) -> Element {
|
|
|
|
|
|
+fn BlogPost(cx: Scope, query_params: ManualBlogQuerySegments) -> Element {
|
|
render! {
|
|
render! {
|
|
div{"This is your blogpost with a query segment:"}
|
|
div{"This is your blogpost with a query segment:"}
|
|
div{format!("{:?}", query_params)}
|
|
div{format!("{:?}", query_params)}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#[component]
|
|
|
|
+fn AutomaticBlogPost(cx: Scope, name: String, surname: String) -> Element {
|
|
|
|
+ render! {
|
|
|
|
+ div{"This is your blogpost with a query segment:"}
|
|
|
|
+ div{format!("name={}&surname={}", name, surname)}
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
#[component]
|
|
#[component]
|
|
fn App(cx: Scope) -> Element {
|
|
fn App(cx: Scope) -> Element {
|
|
render! { Router::<Route>{} }
|
|
render! { Router::<Route>{} }
|