web_router.rs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #![cfg(target_arch = "wasm32")]
  2. use dioxus_core::prelude::*;
  3. use dioxus_core_macro::*;
  4. use dioxus_html as dioxus_elements;
  5. use dioxus_router::*;
  6. use gloo_utils::document;
  7. use serde::{Deserialize, Serialize};
  8. use wasm_bindgen_test::*;
  9. wasm_bindgen_test_configure!(run_in_browser);
  10. #[wasm_bindgen_test]
  11. fn simple_test() {
  12. fn main() {
  13. console_error_panic_hook::set_once();
  14. wasm_logger::init(wasm_logger::Config::new(log::Level::Debug));
  15. dioxus_web::launch(APP);
  16. }
  17. static APP: Component = |cx| {
  18. cx.render(rsx! {
  19. Router {
  20. onchange: move |route: RouterService| log::info!("route changed to {:?}", route.current_location()),
  21. active_class: "is-active",
  22. Route { to: "/", Home {} }
  23. Route { to: "blog"
  24. Route { to: "/", BlogList {} }
  25. Route { to: ":id", BlogPost {} }
  26. }
  27. }
  28. })
  29. };
  30. fn Home(cx: Scope) -> Element {
  31. cx.render(rsx! {
  32. div {
  33. h1 { "Home" }
  34. }
  35. })
  36. }
  37. fn BlogList(cx: Scope) -> Element {
  38. cx.render(rsx! {
  39. div {
  40. }
  41. })
  42. }
  43. fn BlogPost(cx: Scope) -> Element {
  44. let id = use_route(&cx).parse_segment::<usize>("id")?;
  45. cx.render(rsx! {
  46. div {
  47. }
  48. })
  49. }
  50. main();
  51. let element = gloo_utils::document();
  52. }