rust wasm sycamore demo

卓能文發表於2024-07-26

Cargo.toml:

[package]
name = "wasmdemo"
version = "0.1.0"
edition = "2021"

[dependencies]
sycamore = "0.9.0-beta.2"

src/main.rs:

use sycamore::prelude::*;

#[component]
fn App<G: Html>() -> View<G> {
    let mut counter = create_signal(0i32);
    let increment = move |_| counter += 1;
    let decrement = move |_| counter -= 1;
    let reset = move |_| counter.set(0);
    view! {
        div {
            p { "Value: " (counter.get()) }
            button(on:click=increment) { "+" }
            button(on:click=decrement) { "-" }
            button(on:click=reset) { "Reset" }
        }
    }
}

fn main() {
    sycamore::render(App);
}

index.html:

<!doctype html>
<html>

<head>
    <meta charset="utf-8" />
    <title>My first Sycamore app</title>
</head>

<body></body>

</html>

run:

trunk serve

相關文章