simple_desktop.rs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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: "/blog/james",
  22. li { "james amazing' blog" }
  23. }
  24. Link { to: "/apples", li { "go to apples" } }
  25. }
  26. Route { to: "/", Home {} }
  27. Route { to: "/blog/", BlogList {} }
  28. Route { to: "/blog/:id/", BlogPost {} }
  29. Route { to: "/oranges", "Oranges are not apples!" }
  30. Redirect { from: "/apples", to: "/oranges" }
  31. }
  32. })
  33. }
  34. fn Home(cx: Scope) -> Element {
  35. log::debug!("rendering home {:?}", cx.scope_id());
  36. cx.render(rsx! { h1 { "Home" } })
  37. }
  38. fn BlogList(cx: Scope) -> Element {
  39. log::debug!("rendering blog list {:?}", cx.scope_id());
  40. cx.render(rsx! { div { "Blog List" } })
  41. }
  42. fn BlogPost(cx: Scope) -> Element {
  43. let Some(id) = use_route(cx).segment("id") else {
  44. return cx.render(rsx! { div { "No blog post id" } })
  45. };
  46. log::debug!("rendering blog post {}", id);
  47. cx.render(rsx! {
  48. div {
  49. h3 { "blog post: {id:?}" }
  50. Link { to: "/blog/", "back to blog list" }
  51. }
  52. })
  53. }