simple.rs 991 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. use dioxus_core::prelude::*;
  2. use dioxus_core_macro::*;
  3. use dioxus_html as dioxus_elements;
  4. use dioxus_router::*;
  5. fn main() {
  6. console_error_panic_hook::set_once();
  7. dioxus_web::launch(App, |c| c);
  8. }
  9. #[derive(Clone, Debug, PartialEq)]
  10. enum Route {
  11. Home,
  12. About,
  13. NotFound,
  14. }
  15. static App: FC<()> = |cx, props| {
  16. let route = use_router(cx, Route::parse);
  17. match route {
  18. Route::Home => rsx!(cx, div { "Home" }),
  19. Route::About => rsx!(cx, div { "About" }),
  20. Route::NotFound => rsx!(cx, div { "NotFound" }),
  21. }
  22. };
  23. impl ToString for Route {
  24. fn to_string(&self) -> String {
  25. match self {
  26. Route::Home => "/".to_string(),
  27. Route::About => "/about".to_string(),
  28. Route::NotFound => "/404".to_string(),
  29. }
  30. }
  31. }
  32. impl Route {
  33. fn parse(s: &str) -> Self {
  34. match s {
  35. "/" => Route::Home,
  36. "/about" => Route::About,
  37. _ => Route::NotFound,
  38. }
  39. }
  40. }