simple.rs 954 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #![allow(non_snake_case)]
  2. use dioxus_core::prelude::*;
  3. use dioxus_core_macro::*;
  4. use dioxus_html as dioxus_elements;
  5. use dioxus_router::*;
  6. fn main() {
  7. console_error_panic_hook::set_once();
  8. wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
  9. dioxus_web::launch(APP);
  10. }
  11. static APP: Component = |cx| {
  12. cx.render(rsx! {
  13. Router {
  14. onchange: move |route| log::info!("route changed to {}", route),
  15. Route { to: "/", Home {} }
  16. Route { to: "blog"
  17. Route { to: "/", BlogList {} }
  18. Route { to: ":id", BlogPost {} }
  19. }
  20. }
  21. })
  22. };
  23. fn Home(cx: Scope) -> Element {
  24. cx.render(rsx! {
  25. h1 { "Home" }
  26. })
  27. }
  28. fn BlogList(cx: Scope) -> Element {
  29. cx.render(rsx! {
  30. div { "Blog List" }
  31. })
  32. }
  33. fn BlogPost(cx: Scope) -> Element {
  34. let id = use_route(&cx).segment::<usize>("id")?;
  35. cx.render(rsx! { div { "{id:?}" } })
  36. }