2024-02-14 21:48:58 +00:00
|
|
|
//! A simple example of a router with a few routes and a nav bar.
|
2024-01-20 00:36:40 +00:00
|
|
|
|
2023-07-27 02:19:51 +00:00
|
|
|
use dioxus::prelude::*;
|
|
|
|
|
2024-02-14 21:48:58 +00:00
|
|
|
fn main() {
|
2024-10-10 23:00:58 +00:00
|
|
|
// launch the router, using our `Route` component as the generic type
|
2024-02-14 21:48:58 +00:00
|
|
|
// This will automatically boot the app to "/" unless otherwise specified
|
2024-10-10 23:00:58 +00:00
|
|
|
dioxus::launch(|| rsx! { Router::<Route> {} });
|
2024-02-14 21:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// By default, the Routable derive will use the name of the variant as the route
|
|
|
|
/// You can also specify a specific component by adding the Component name to the `#[route]` attribute
|
|
|
|
#[rustfmt::skip]
|
2023-07-27 02:19:51 +00:00
|
|
|
#[derive(Routable, Clone, PartialEq)]
|
|
|
|
enum Route {
|
2024-02-14 21:48:58 +00:00
|
|
|
// Wrap the app in a Nav layout
|
2023-07-27 02:19:51 +00:00
|
|
|
#[layout(Nav)]
|
2024-02-14 21:48:58 +00:00
|
|
|
#[route("/")]
|
|
|
|
Homepage {},
|
2023-07-27 02:19:51 +00:00
|
|
|
|
2024-02-14 21:48:58 +00:00
|
|
|
#[route("/blog/:id")]
|
|
|
|
Blog { id: String },
|
2023-07-27 02:19:51 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 14:13:36 +00:00
|
|
|
#[component]
|
2024-01-14 04:51:37 +00:00
|
|
|
fn Homepage() -> Element {
|
2024-03-20 16:04:37 +00:00
|
|
|
rsx! { h1 { "Welcome home" } }
|
2023-07-27 02:19:51 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 14:13:36 +00:00
|
|
|
#[component]
|
2024-01-14 04:51:37 +00:00
|
|
|
fn Blog(id: String) -> Element {
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2023-07-27 02:19:51 +00:00
|
|
|
h1 { "How to make: " }
|
|
|
|
p { "{id}" }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-14 21:48:58 +00:00
|
|
|
/// A simple nav bar that links to the homepage and blog pages
|
|
|
|
///
|
|
|
|
/// The `Route` enum gives up typesafe routes, allowing us to rename routes and serialize them automatically
|
2023-09-15 14:13:36 +00:00
|
|
|
#[component]
|
2024-01-14 04:51:37 +00:00
|
|
|
fn Nav() -> Element {
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2023-07-27 02:19:51 +00:00
|
|
|
nav {
|
2024-01-16 15:40:17 +00:00
|
|
|
li {
|
|
|
|
Link { to: Route::Homepage {}, "Go home" }
|
|
|
|
}
|
|
|
|
li {
|
|
|
|
Link {
|
|
|
|
to: Route::Blog {
|
|
|
|
id: "Brownies".to_string(),
|
|
|
|
},
|
2024-03-20 16:04:37 +00:00
|
|
|
onclick: move |_| { println!("Clicked on Brownies") },
|
2024-01-16 15:40:17 +00:00
|
|
|
"Learn Brownies"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
li {
|
|
|
|
Link {
|
|
|
|
to: Route::Blog {
|
|
|
|
id: "Cookies".to_string(),
|
|
|
|
},
|
|
|
|
"Learn Cookies"
|
|
|
|
}
|
|
|
|
}
|
2023-07-27 02:19:51 +00:00
|
|
|
}
|
|
|
|
div { Outlet::<Route> {} }
|
|
|
|
}
|
|
|
|
}
|