dioxus/packages/router/examples/simple_routes.rs

82 lines
2 KiB
Rust
Raw Normal View History

2022-12-09 00:15:04 +00:00
#![allow(non_snake_case)]
use dioxus::prelude::*;
2022-12-15 19:09:53 +00:00
use dioxus_router::{history::MemoryHistory, prelude::*};
2022-12-09 00:15:04 +00:00
fn main() {
2022-12-15 13:46:40 +00:00
#[cfg(not(feature = "web"))]
2022-12-09 00:15:04 +00:00
dioxus_desktop::launch(App);
2022-12-15 13:46:40 +00:00
#[cfg(feature = "web")]
dioxus_web::launch(App);
2022-12-09 00:15:04 +00:00
}
fn App(cx: Scope) -> Element {
use_router(
2023-04-12 18:19:01 +00:00
cx,
2022-12-15 19:09:53 +00:00
&|| {
#[cfg(not(feature = "web"))]
let history = MemoryHistory::default();
#[cfg(feature = "web")]
let history = dioxus_router::history::WebHistory::new(None, true);
RouterConfiguration {
history: Box::new(history),
..Default::default()
}
2022-12-09 00:15:04 +00:00
},
&|| {
Segment::content(comp(Home))
.fixed("apple", comp(Apple))
.fixed("potato", Route::content(comp(Potato)).name::<PotatoName>())
.fixed("earth_apple", named::<PotatoName>())
},
2022-12-09 00:15:04 +00:00
);
render! {
2022-12-14 17:10:36 +00:00
h1 { "Simple Example App" }
Outlet { }
Link {
target: named::<RootIndex>(),
"Go to root"
2022-12-09 03:11:31 +00:00
}
2022-12-09 00:15:04 +00:00
}
}
fn Home(cx: Scope) -> Element {
2022-12-09 00:15:04 +00:00
render! {
2022-12-09 03:11:31 +00:00
h2 { "Root Index" }
ul {
li { Link {
target: "/apple",
"Read about apples…"
} }
li { Link {
target: named::<PotatoName>(),
"Read about potatoes…"
} }
li { Link {
target: "/earth_apple",
"Read about earth apples (literal translation of a german word for potato)…"
} }
2022-12-09 03:11:31 +00:00
}
}
}
fn Apple(cx: Scope) -> Element {
render! {
h2 { "Apple" }
p {
"An apple is a tasty fruit. It grows on trees and many varieties are either red or "
"green."
}
2022-12-09 00:15:04 +00:00
}
}
struct PotatoName;
fn Potato(cx: Scope) -> Element {
render! {
h2 { "Potato" }
p { "The potato grows underground. There are many recipes involving potatoes." }
}
}