simple.rs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #![allow(non_snake_case)]
  2. use dioxus::prelude::*;
  3. use dioxus_router::prelude::*;
  4. fn main() {
  5. #[cfg(not(feature = "web"))]
  6. dioxus_desktop::launch(App);
  7. #[cfg(feature = "web")]
  8. dioxus_web::launch(App);
  9. }
  10. fn App(cx: Scope) -> Element {
  11. use_router(
  12. &cx,
  13. &|| RouterConfiguration {
  14. ..Default::default()
  15. },
  16. &|| {
  17. Segment::content(comp(Home))
  18. .fixed("apple", comp(Apple))
  19. .fixed("potato", Route::content(comp(Potato)).name::<PotatoName>())
  20. .fixed("earth_apple", named::<PotatoName>())
  21. },
  22. );
  23. render! {
  24. h1 { "Simple Example App" }
  25. Outlet { }
  26. Link {
  27. target: named::<RootIndex>(),
  28. "Go to root"
  29. }
  30. }
  31. }
  32. fn Home(cx: Scope) -> Element {
  33. render! {
  34. h2 { "Root Index" }
  35. ul {
  36. li { Link {
  37. target: "/apple",
  38. "Read about apples…"
  39. } }
  40. li { Link {
  41. target: named::<PotatoName>(),
  42. "Read about potatoes…"
  43. } }
  44. li { Link {
  45. target: "/earth_apple",
  46. "Read about earth apples (literal translation of a german word for potato)…"
  47. } }
  48. }
  49. }
  50. }
  51. fn Apple(cx: Scope) -> Element {
  52. render! {
  53. h2 { "Apple" }
  54. p {
  55. "An apple is a tasty fruit. It grows on trees and many varieties are either red or "
  56. "green."
  57. }
  58. }
  59. }
  60. struct PotatoName;
  61. fn Potato(cx: Scope) -> Element {
  62. render! {
  63. h2 { "Potato" }
  64. p { "The potato grows underground. There are many recipes involving potatoes." }
  65. }
  66. }