rust yew使用教程(二)

3 篇文章 0 订阅
序言

    依旧使用例子演示,例子包含定义组件,以及将组件点击事件传递给上一个组件处理,例子如下:

src/btn_widget.rs
use yew::prelude::*;

pub struct MyButton {
    link: ComponentLink<Self>,
    props: Props,
}

#[derive(PartialEq, Properties, Clone)]
pub struct Props {
    pub txt: String,
    pub callback: Callback<()>,
}

pub enum Msg {
    Click,
}

impl Component for MyButton {
    type Message = Msg;
    type Properties = Props;

    fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
        Self { link, props }
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            Msg::Click => {
                self.props.callback.emit(());
            }
        }
        true
    }

    fn change(&mut self, props: Self::Properties) -> ShouldRender {
        // 组件必须包含下面代码
        self.props = props;
        true
    }

    fn view(&self) -> Html {
        html! {
            <button class={"button"} onclick=self.link.callback(|_| Msg::Click)>{ self.props.txt.clone() }</button>
        }
    }
}
src/main.rs
use yew::prelude::*;
mod btn_widget;

use btn_widget::MyButton;

enum Msg {
    AddOne,
    AddTwo,
}

struct Model {
    link: ComponentLink<Self>,
    item_type: i32,
}

impl Model {
    fn btn_click(&mut self) {
        // js_api::js_alert("hello from wasm!");
        self.item_type = 1;
    }

    fn btn_two_click(&mut self) {
        self.item_type = 0;
    }

    fn hello_world_ui(&self) -> Html {
        html! {
            <div>
            <center>
                <h1>{"布局以被切换"}</h1>
                <button class={"button"} onclick=self.link.callback(|_| Msg::AddTwo)>{ "点击 上一个布局" }</button>
            </center>
            </div>
        }
    }

    fn def_ui(&self) -> Html {
        html! {
            <div>
            <center>
                <p>{ "yew 测试实例一" }</p>
                <MyButton txt={String::from("点击 下一个布局")} callback=self.link.callback(|_| Msg::AddOne) />
                // <button class={"button"} οnclick=self.link.callback(|_| Msg::AddOne)>{ "点击 下一个布局" }</button>
                // <button class={"button"} οnclick=self.link.callback(|_| Msg::AddOne)>{ "点击 下一个布局" }</button>
            </center>
            </div>
        }
    }
}

impl Component for Model {
    type Message = Msg;
    type Properties = ();

    fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self {
        Self { link, item_type: 0 }
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            Msg::AddOne => {
                self.btn_click();
                true
            }
            Msg::AddTwo => {
                self.btn_two_click();
                true
            }
        }
    }

    fn change(&mut self, _props: Self::Properties) -> ShouldRender {
        false
    }

    fn view(&self) -> Html {
        match self.item_type {
            0 => self.def_ui(),
            _ => self.hello_world_ui(),
        }
    }
}

fn main() {
    yew::start_app::<Model>();
}

总结

    比较难的点在于点击事件如何传递,毕竟rust和其它语言不同,之前使用过Box等智能指针,但是发现只有Rc符合使用场景,由于官方文档并没有关于Callback的介绍,最终在某个demo中找到对应的解决方式,和我构想类似,这个例子比较简单,主要演示yew和react的思想与使用接近一致(别人说的),当然yew的组件化思想比较好,当然缺点还是有的比如写html代码没有提示;Msg主要作用是定义事件的响应,Props 用于数据传递或者说是需要外部传递的数据,没有可以不写,yew组件必须实现Component这个接口,每一个函数都是对应生命周期具体的就不展开了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Rust 中使用 Yew 框架编写 Function Component 非常简单。Function Component 是一种无状态组件,只接收 props 参数并返回一个 Virtual DOM 树。 下面是一个简单的例子,演示如何编写一个 Function Component: ```rust use yew::prelude::*; fn function_component(props: &Props) -> Html { html! { <div> <h1>{ props.title }</h1> <p>{ props.content }</p> </div> } } #[derive(Clone, PartialEq, Properties)] struct Props { title: String, content: String, } ``` 在这个例子中,我们定义了一个名为 `function_component` 的函数组件,并接受一个 `props` 参数。在函数组件中,我们使用 `html!` 宏来创建 Virtual DOM 树,并将 `props` 中的 `title` 和 `content` 属性设置为标题和段落的文本内容。 为了使用这个组件,我们需要在父组件中将 `Props` 传递给 `function_component` 函数,并将其渲染到页面上: ```rust use yew::prelude::*; struct App {} impl Component for App { type Message = (); type Properties = (); fn create(_: Self::Properties, _: ComponentLink<Self>) -> Self { App {} } fn update(&mut self, _: Self::Message) -> ShouldRender { false } fn view(&self) -> Html { let props = Props { title: String::from("Hello, World!"), content: String::from("This is a Yew Function Component."), }; function_component(&props) } } ``` 在这个例子中,我们创建了一个名为 `App` 的组件,并在 `view` 方法中将 `Props` 传递给 `function_component` 函数。在实际应用中,我们可以根据需要设置不同的 `Props` 属性来渲染不同的页面内容。 希望这个例子对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值