17次重大发布

0.18.0 2023年9月29日
0.17.0 2022年11月25日
0.16.0 2021年12月11日
0.15.0 2021年5月15日
0.3.0 2019年7月24日

#5 in #single-page

Download history 3730/week @ 2024-04-08 4756/week @ 2024-04-15 4323/week @ 2024-04-22 3900/week @ 2024-04-29 4075/week @ 2024-05-06 4091/week @ 2024-05-13 4058/week @ 2024-05-20 3638/week @ 2024-05-27 4804/week @ 2024-06-03 3610/week @ 2024-06-10 3837/week @ 2024-06-17 4104/week @ 2024-06-24 3684/week @ 2024-07-01 3601/week @ 2024-07-08 4353/week @ 2024-07-15 4845/week @ 2024-07-22

16,881 monthly downloads
Used in 26 crates (22 directly)

MIT/Apache

610KB
14K SLoC

yew-router

Yew 前端框架的路由库。

阅读如何在 yew.rs 上使用它


lib.rs:

提供使用浏览器历史API构建单页面应用(SPA)的路由功能,使用 Yew 网页框架

用法

use yew::functional::*;
use yew::prelude::*;
use yew_router::prelude::*;

#[derive(Debug, Clone, Copy, PartialEq, Routable)]
enum Route {
    #[at("/")]
    Home,
    #[at("/secure")]
    Secure,
    #[not_found]
    #[at("/404")]
    NotFound,
}

#[function_component(Secure)]
fn secure() -> Html {
    let navigator = use_navigator().unwrap();

    let onclick_callback = Callback::from(move |_| navigator.push(&Route::Home));
    html! {
        <div>
            <h1>{ "Secure" }</h1>
            <button onclick={onclick_callback}>{ "Go Home" }</button>
        </div>
    }
}

#[function_component(Main)]
fn app() -> Html {
    html! {
        <BrowserRouter>
            <Switch<Route> render={switch} />
        </BrowserRouter>
    }
}

fn switch(routes: Route) -> Html {
    match routes {
        Route::Home => html! { <h1>{ "Home" }</h1> },
        Route::Secure => html! {
            <Secure />
        },
        Route::NotFound => html! { <h1>{ "404" }</h1> },
    }
}

内部结构

路由器将自己注册为上下文提供者,并通过 hooksRouterScopeExt 使位置信息和导航器可用。

状态

Location API提供了一种访问/存储与会话历史记录相关联的状态的方法。有关详细用法,请参阅location.state()。这是一个提供通用会话历史记录和位置信息的模块。导入 yew-router 时要导入的预置模块。

此模块重新导出从包中常用的类型。

依赖项

~11–15MB
~277K SLoC