simple.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. fn app(cx: Scope) -> Element {
  12. cx.render(rsx! {
  13. Router {
  14. h1 { "Your app here" }
  15. ul {
  16. Link { to: "/", li { "home" }}
  17. Link { to: "/blog", li { "blog" }}
  18. Link { to: "/blog/tim", li { "tims' blog" }}
  19. Link { to: "/blog/bill", li { "bills' blog" }}
  20. Link { to: "/apples", li { "go to apples" }}
  21. }
  22. Route { to: "/", Home {} }
  23. Route { to: "/blog/", BlogList {} }
  24. Route { to: "/blog/:id/", BlogPost {} }
  25. Route { to: "/oranges", "Oranges are not apples!" }
  26. Redirect { from: "/apples", to: "/oranges" }
  27. }
  28. })
  29. }
  30. fn Home(cx: Scope) -> Element {
  31. cx.render(rsx! {
  32. h1 { "Home" }
  33. })
  34. }
  35. fn BlogList(cx: Scope) -> Element {
  36. cx.render(rsx! {
  37. div { "Blog List" }
  38. })
  39. }
  40. fn BlogPost(cx: Scope) -> Element {
  41. let id = use_route(&cx).segment("id")?;
  42. log::trace!("rendering blog post {}", id);
  43. cx.render(rsx! { div { "{id:?}" } })
  44. }