simple.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #![allow(non_snake_case)]
  2. use dioxus::prelude::*;
  3. use dioxus_router::*;
  4. fn main() {
  5. console_error_panic_hook::set_once();
  6. wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
  7. dioxus_web::launch(app);
  8. }
  9. fn app(cx: Scope) -> Element {
  10. cx.render(rsx! {
  11. Router {
  12. h1 { "Your app here" }
  13. ul {
  14. Link { to: "/", li { "home" } }
  15. Link { to: "/blog", li { "blog" } }
  16. Link { to: "/blog/tim", li { "tims' blog" } }
  17. Link { to: "/blog/bill", li { "bills' blog" } }
  18. Link { to: "/apples", li { "go to apples" } }
  19. }
  20. Route { to: "/", Home {} }
  21. Route { to: "/blog/", BlogList {} }
  22. Route { to: "/blog/:id/", BlogPost {} }
  23. Route { to: "/oranges", "Oranges are not apples!" }
  24. Redirect { from: "/apples", to: "/oranges" }
  25. }
  26. })
  27. }
  28. fn Home(cx: Scope) -> Element {
  29. cx.render(rsx! { h1 { "Home" } })
  30. }
  31. fn BlogList(cx: Scope) -> Element {
  32. cx.render(rsx! { div { "Blog List" } })
  33. }
  34. fn BlogPost(cx: Scope) -> Element {
  35. let id = use_route(&cx).segment("id")?;
  36. log::trace!("rendering blog post {}", id);
  37. cx.render(rsx! { div { "{id:?}" } })
  38. }