router.rs 3.5 KB

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