首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将路由定义为组件的支柱

如何将路由定义为组件的支柱
EN

Stack Overflow用户
提问于 2022-05-07 15:39:11
回答 1查看 126关注 0票数 1

我定义了以下路线。

代码语言:javascript
复制
#[derive(Routable, PartialEq, Debug, Clone)]
pub enum Route {
    #[at("/")]
    Index,

    #[at("about")]
    About,

    #[at("/contact")]
    Contact,

    #[at("/portfolio")]
    Portfolio,

    #[at("*")]
    NotFound,
}

我想传递一条路由,例如Route::Index到下面的组件,但是有一个错误,如注释中所示

代码语言:javascript
复制
#[derive(Properties, PartialEq)]
pub struct IconProps {
    pub route: Route,
    pub alt: String,
    pub icon_name: String,
}

#[function_component(Icon)]
pub fn icon(props: &IconProps) -> Html {
    let src = format!("assets/icons/{}.svg", props.icon_name);

    html! {
        <div class={classes!("p-2", "mx-2", "my-1", "bg-green-100", "inline-block")}>
            <Link<Route> classes={classes!("w-10", "h-10")} to={props.route}>
                                                               ^^^^^^^^^^^^^^
// Diagnostics:
// 1. cannot move out of `props.route` which is behind a shared reference
//   move occurs because `props.route` has type `Route`, which does not implement the `Copy` trait

                <img
                    src={src}
                    alt={props.alt.to_owned()}
                    class={classes!("w-10", "h-10")}
                />
            </Link<Route>>
        </div>
    }
}

那么,怎么把这条路当作道具呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-07 16:09:23

嗯,解决办法有点明显,我想。只需将Copy特性添加到enum

代码语言:javascript
复制
#[derive(Routable, PartialEq, Debug, Clone, Copy)]
pub enum Route {
    #[at("/")]
    Index,

    #[at("about")]
    About,

    #[at("/contact")]
    Contact,

    #[at("/portfolio")]
    Portfolio,

    #[at("*")]
    NotFound,
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72154018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档