nested_routes.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #![allow(non_snake_case)]
  2. use dioxus::prelude::*;
  3. use dioxus_router::prelude::*;
  4. // ANCHOR: router
  5. #[derive(Routable, Clone)]
  6. #[rustfmt::skip]
  7. enum Route {
  8. // All routes under the NavBar layout will be rendered inside of the NavBar Outlet
  9. #[layout(NavBar)]
  10. #[route("/")]
  11. Home {},
  12. #[end_layout]
  13. #[route("/:..route")]
  14. PageNotFound { route: Vec<String> },
  15. }
  16. // ANCHOR_END: router
  17. // ANCHOR: nav
  18. #[inline_props]
  19. fn NavBar(cx: Scope) -> Element {
  20. render! {
  21. nav {
  22. ul {
  23. li { "links" }
  24. }
  25. }
  26. // The Outlet component will render child routes (In this case just the Home component) inside the Outlet component
  27. Outlet::<Route> {}
  28. }
  29. }
  30. // ANCHOR_END: nav
  31. // ANCHOR: app
  32. #[inline_props]
  33. fn App(cx: Scope) -> Element {
  34. render! {
  35. Router::<Route> {}
  36. }
  37. }
  38. // ANCHOR_END: app
  39. // ANCHOR: home
  40. #[inline_props]
  41. fn Home(cx: Scope) -> Element {
  42. render! {
  43. h1 { "Welcome to the Dioxus Blog!" }
  44. }
  45. }
  46. // ANCHOR_END: home
  47. // ANCHOR: fallback
  48. #[inline_props]
  49. fn PageNotFound(cx: Scope, route: Vec<String>) -> Element {
  50. render! {
  51. h1 { "Page not found" }
  52. p { "We are terribly sorry, but the page you requested doesn't exist." }
  53. pre {
  54. color: "red",
  55. "log:\nattemped to navigate to: {route:?}"
  56. }
  57. }
  58. }
  59. // ANCHOR_END: fallback
  60. fn main() {}