router.rs 3.6 KB

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