links.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 {
  24. Link {
  25. // The Link component will navigate to the route specified
  26. // in the target prop which is checked to exist at compile time
  27. to: Route::Home {},
  28. "Home"
  29. }
  30. }
  31. }
  32. }
  33. Outlet {}
  34. }
  35. }
  36. // ANCHOR_END: nav
  37. // ANCHOR: app
  38. #[inline_props]
  39. fn App(cx: Scope) -> Element {
  40. render! {
  41. Router {}
  42. }
  43. }
  44. // ANCHOR_END: app
  45. // ANCHOR: home
  46. #[inline_props]
  47. fn Home(cx: Scope) -> Element {
  48. render! {
  49. h1 { "Welcome to the Dioxus Blog!" }
  50. }
  51. }
  52. // ANCHOR_END: home
  53. // ANCHOR: fallback
  54. #[inline_props]
  55. fn PageNotFound(cx: Scope, route: Vec<String>) -> Element {
  56. render! {
  57. h1 { "Page not found" }
  58. p { "We are terribly sorry, but the page you requested doesn't exist." }
  59. pre {
  60. color: "red",
  61. "log:\nattemped to navigate to: {route:?}"
  62. }
  63. }
  64. }
  65. // ANCHOR_END: fallback
  66. fn main() {}