simple_desktop.rs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. use dioxus::prelude::*;
  2. use dioxus_router::*;
  3. fn main() {
  4. simple_logger::SimpleLogger::new()
  5. .with_level(log::LevelFilter::Debug)
  6. .with_module_level("dioxus_router", log::LevelFilter::Trace)
  7. .with_module_level("dioxus", log::LevelFilter::Trace)
  8. .init()
  9. .unwrap();
  10. dioxus_desktop::launch(app);
  11. }
  12. fn app(cx: Scope) -> Element {
  13. cx.render(rsx! {
  14. Router {
  15. h1 { "Your app here" }
  16. ul {
  17. Link { to: "/", li { "home" } }
  18. Link { to: "/blog", li { "blog" } }
  19. Link { to: "/blog/tim", li { "tims' blog" } }
  20. Link { to: "/blog/bill", li { "bills' blog" } }
  21. Link { to: "/apples", li { "go to apples" } }
  22. }
  23. Route { to: "/", Home {} }
  24. Route { to: "/blog/", BlogList {} }
  25. Route { to: "/blog/:id/", BlogPost {} }
  26. Route { to: "/oranges", "Oranges are not apples!" }
  27. Redirect { from: "/apples", to: "/oranges" }
  28. }
  29. })
  30. }
  31. fn Home(cx: Scope) -> Element {
  32. log::debug!("rendering home {:?}", cx.scope_id());
  33. cx.render(rsx! { h1 { "Home" } })
  34. }
  35. fn BlogList(cx: Scope) -> Element {
  36. log::debug!("rendering blog list {:?}", cx.scope_id());
  37. cx.render(rsx! { div { "Blog List" } })
  38. }
  39. fn BlogPost(cx: Scope) -> Element {
  40. let Some(id) = use_route(cx).segment("id") else {
  41. return cx.render(rsx! { div { "No blog post id" } })
  42. };
  43. log::debug!("rendering blog post {}", id);
  44. cx.render(rsx! { div { "{id:?}" } })
  45. }