Started with yew router
This commit is contained in:
parent
3243f5d79b
commit
a42d8557a8
2 changed files with 42 additions and 26 deletions
|
@ -1,44 +1,60 @@
|
|||
#![recursion_limit = "256"]
|
||||
|
||||
mod component;
|
||||
|
||||
use component::login::LoginComponent;
|
||||
use yew::prelude::*;
|
||||
use yew::virtual_dom::VNode;
|
||||
use yew_router::{prelude::*, switch::Permissive, Switch};
|
||||
|
||||
struct App {
|
||||
clicked: bool,
|
||||
onclick: Callback<ClickEvent>,
|
||||
}
|
||||
struct App {}
|
||||
|
||||
enum Msg {
|
||||
Click,
|
||||
#[derive(Debug, Switch, Clone)]
|
||||
enum AppRoute {
|
||||
#[to = "/"]
|
||||
Root,
|
||||
#[to = "/login"]
|
||||
Login,
|
||||
PageNotFound(Permissive<String>),
|
||||
}
|
||||
|
||||
impl Component for App {
|
||||
type Message = Msg;
|
||||
type Message = ();
|
||||
type Properties = ();
|
||||
|
||||
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
|
||||
App {
|
||||
clicked: false,
|
||||
onclick: link.callback(|_| Msg::Click),
|
||||
}
|
||||
fn create(_: Self::Properties, _link: ComponentLink<Self>) -> Self {
|
||||
App {}
|
||||
}
|
||||
|
||||
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
||||
match msg {
|
||||
Msg::Click => {
|
||||
if self.clicked {
|
||||
self.clicked = false;
|
||||
} else {
|
||||
self.clicked = true
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
fn update(&mut self, _msg: Self::Message) -> ShouldRender {
|
||||
true
|
||||
}
|
||||
|
||||
fn view(&self) -> Html {
|
||||
fn view(&self) -> VNode {
|
||||
html! {
|
||||
<LoginComponent:/>
|
||||
<div>
|
||||
<nav class="menu",>
|
||||
<RouterButton<AppRoute> route=AppRoute::Root>{"Go to Root"}</RouterButton<AppRoute>>
|
||||
<RouterButton<AppRoute> route=AppRoute::Login>{"Go to Login"}</RouterButton<AppRoute>>
|
||||
</nav>
|
||||
<div>
|
||||
<Router<AppRoute>
|
||||
render = Router::render(|switch: AppRoute| {
|
||||
match switch {
|
||||
AppRoute::Login => html!{<LoginComponent />},
|
||||
AppRoute::Root => {
|
||||
html!{"hello there!"}
|
||||
},
|
||||
AppRoute::PageNotFound(Permissive(None)) => html!{"Page not found"},
|
||||
AppRoute::PageNotFound(Permissive(Some(missed_route))) => html!{format!("Page '{}' not found", missed_route)}
|
||||
}
|
||||
})
|
||||
redirect = Router::redirect(|route: Route| {
|
||||
AppRoute::PageNotFound(Permissive(Some(route.route)))
|
||||
})
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue