simple.rs 2.0 KB

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