router.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //! An advanced usage of the router with nested routes and redirects.
  2. //!
  3. //! Dioxus implements an enum-based router, which allows you to define your routes in a type-safe way.
  4. //! However, since we need to bake quite a bit of logic into the enum, we have to add some extra syntax.
  5. //!
  6. //! Note that you don't need to use advanced features like nest, redirect, etc, since these can all be implemented
  7. //! manually, but they are provided as a convenience.
  8. use dioxus::prelude::*;
  9. fn main() {
  10. launch(|| {
  11. rsx! {
  12. style { {include_str!("./assets/router.css")} }
  13. Router::<Route> {}
  14. }
  15. });
  16. }
  17. // Turn off rustfmt since we're doing layouts and routes in the same enum
  18. #[derive(Routable, Clone, Debug, PartialEq)]
  19. #[rustfmt::skip]
  20. enum Route {
  21. // Wrap Home in a Navbar Layout
  22. #[layout(NavBar)]
  23. // The default route is always "/" unless otherwise specified
  24. #[route("/")]
  25. Home {},
  26. // Wrap the next routes in a layout and a nest
  27. #[nest("/blog")]
  28. #[layout(Blog)]
  29. // At "/blog", we want to show a list of blog posts
  30. #[route("/")]
  31. BlogList {},
  32. // At "/blog/:name", we want to show a specific blog post, using the name slug
  33. #[route("/:name")]
  34. BlogPost { name: String },
  35. // We need to end the blog layout and nest
  36. // Note we don't need either - we could've just done `/blog/` and `/blog/:name` without nesting,
  37. // but it's a bit cleaner this way
  38. #[end_layout]
  39. #[end_nest]
  40. // And the regular page layout
  41. #[end_layout]
  42. // Add some redirects for the `/myblog` route
  43. #[nest("/myblog")]
  44. #[redirect("/", || Route::BlogList {})]
  45. #[redirect("/:name", |name: String| Route::BlogPost { name })]
  46. #[end_nest]
  47. // Finally, we need to handle the 404 page
  48. #[route("/:..route")]
  49. PageNotFound {
  50. route: Vec<String>,
  51. },
  52. }
  53. #[component]
  54. fn NavBar() -> Element {
  55. rsx! {
  56. nav { id: "navbar",
  57. Link { to: Route::Home {}, "Home" }
  58. Link { to: Route::BlogList {}, "Blog" }
  59. }
  60. Outlet::<Route> {}
  61. }
  62. }
  63. #[component]
  64. fn Home() -> Element {
  65. rsx! { h1 { "Welcome to the Dioxus Blog!" } }
  66. }
  67. #[component]
  68. fn Blog() -> Element {
  69. rsx! {
  70. h1 { "Blog" }
  71. Outlet::<Route> {}
  72. }
  73. }
  74. #[component]
  75. fn BlogList() -> Element {
  76. rsx! {
  77. h2 { "Choose a post" }
  78. div { id: "blog-list",
  79. Link { to: Route::BlogPost { name: "Blog post 1".into() },
  80. "Read the first blog post"
  81. }
  82. Link { to: Route::BlogPost { name: "Blog post 2".into() },
  83. "Read the second blog post"
  84. }
  85. }
  86. }
  87. }
  88. // We can use the `name` slug to show a specific blog post
  89. // In theory we could read from the filesystem or a database here
  90. #[component]
  91. fn BlogPost(name: String) -> Element {
  92. let contents = match name.as_str() {
  93. "Blog post 1" => "This is the first blog post. It's not very interesting.",
  94. "Blog post 2" => "This is the second blog post. It's not very interesting either.",
  95. _ => "This blog post doesn't exist.",
  96. };
  97. rsx! {
  98. h2 { "{name}" }
  99. p { "{contents}" }
  100. }
  101. }
  102. #[component]
  103. fn PageNotFound(route: Vec<String>) -> Element {
  104. rsx! {
  105. h1 { "Page not found" }
  106. p { "We are terribly sorry, but the page you requested doesn't exist." }
  107. pre { color: "red", "log:\nattemped to navigate to: {route:?}" }
  108. }
  109. }