跳到正文

路由

声明路由,在当前窗口中渲染匹配结果,并用 push / replace 导航。

#路由声明

let router = Router::new(
    [
        RouteDefinition::new("home", "/"),
        RouteDefinition::new("project", "/projects/:id"),
    ],
    "/",
)?;

RouteDefinition::layout 声明无路径的祖先。相对子路径会拼到父路径上;绝对子路径从根开始。

#嵌套布局

匹配路由链,并用布局元素包裹内部内容。无路径布局会出现在匹配结果中,但不会增加 URL 片段。

RouteDefinition::layout("shell").parent("root")

#导航

self.router.push("/projects/42")?;
cx.invalidate();

控制器支持 push、replace、back 和 forward。导航留在应用内部。

#路由参数

if let Some(matched) = self.router.matched() {
    let id = matched
        .params()
        .iter()
        .find(|param| param.name() == "id")
        .map(|param| param.value())
        .unwrap_or("");
    text(format!("Project {id}"))
} else {
    text("Page not found")
}