pub trait Routable:
FromStr
+ Display
+ Clone
+ 'static {
const SITE_MAP: &'static [SiteMapSegment];
// Required method
fn render(&self, level: usize) -> Element;
// Provided methods
fn is_child_of(&self, other: &Self) -> bool { ... }
fn parent(&self) -> Option<Self> { ... }
fn flatten_site_map<'a>( ) -> FlatMap<Iter<'a, SiteMapSegment>, Vec<Vec<SegmentType>>, fn(&SiteMapSegment) -> Vec<Vec<SegmentType>>> ⓘ { ... }
fn static_routes() -> Vec<Self> { ... }
}
Expand description
The Routable trait is implemented for types that can be converted to and from a route and be rendered as a page.
A Routable object is something that can be:
- Converted from a route.
- Converted to a route.
- Rendered as a component.
This trait should generally be derived using the dioxus_router_macro::Routable
macro which has more information about the syntax.
§Example
use dioxus::prelude::*;
fn App() -> Element {
rsx! {
Router::<Route> { }
}
}
// Routes are generally enums that derive `Routable`
#[derive(Routable, Clone, PartialEq, Debug)]
enum Route {
// Each enum has an associated url
#[route("/")]
Home {},
// Routes can include dynamic segments that are parsed from the url
#[route("/blog/:blog_id")]
Blog { blog_id: usize },
// Or query segments that are parsed from the url
#[route("/edit?:blog_id")]
Edit { blog_id: usize },
// Or hash segments that are parsed from the url
#[route("/hashtag/#:hash")]
Hash { hash: String },
}
// Each route variant defaults to rendering a component of the same name
#[component]
fn Home() -> Element {
rsx! {
h1 { "Home" }
}
}
// If the variant has dynamic parameters, those are passed to the component
#[component]
fn Blog(blog_id: usize) -> Element {
rsx! {
h1 { "Blog" }
}
}
#[component]
fn Edit(blog_id: usize) -> Element {
rsx! {
h1 { "Edit" }
}
}
#[component]
fn Hash(hash: String) -> Element {
rsx! {
h1 { "Hashtag #{hash}" }
}
}
Required Associated Constants§
Sourceconst SITE_MAP: &'static [SiteMapSegment]
const SITE_MAP: &'static [SiteMapSegment]
The error that can occur when parsing a route.
Required Methods§
Provided Methods§
Sourcefn is_child_of(&self, other: &Self) -> bool
fn is_child_of(&self, other: &Self) -> bool
Checks if this route is a child of the given route.
§Example
use dioxus_router::prelude::*;
use dioxus::prelude::*;
#[component]
fn Home() -> Element { VNode::empty() }
#[component]
fn About() -> Element { VNode::empty() }
#[derive(Routable, Clone, PartialEq, Debug)]
enum Route {
#[route("/")]
Home {},
#[route("/about")]
About {},
}
let route = Route::About {};
let parent = Route::Home {};
assert!(route.is_child_of(&parent));
Sourcefn parent(&self) -> Option<Self>
fn parent(&self) -> Option<Self>
Get the parent route of this route.
§Example
use dioxus_router::prelude::*;
use dioxus::prelude::*;
#[component]
fn Home() -> Element { VNode::empty() }
#[component]
fn About() -> Element { VNode::empty() }
#[derive(Routable, Clone, PartialEq, Debug)]
enum Route {
#[route("/home")]
Home {},
#[route("/home/about")]
About {},
}
let route = Route::About {};
let parent = route.parent().unwrap();
assert_eq!(parent, Route::Home {});
Sourcefn flatten_site_map<'a>() -> FlatMap<Iter<'a, SiteMapSegment>, Vec<Vec<SegmentType>>, fn(&SiteMapSegment) -> Vec<Vec<SegmentType>>> ⓘ
fn flatten_site_map<'a>() -> FlatMap<Iter<'a, SiteMapSegment>, Vec<Vec<SegmentType>>, fn(&SiteMapSegment) -> Vec<Vec<SegmentType>>> ⓘ
Returns a flattened version of Self::SITE_MAP
.
Sourcefn static_routes() -> Vec<Self>
fn static_routes() -> Vec<Self>
Gets a list of all the static routes.
Example static route: #[route("/static/route")]
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.