JS with RS
Yew 的中心思想是将可重用的 UI 所需的所有内容放在一个地方 - Rust 文件中,同时在必要时保持底层技术的可访问性。
截至今天,WebAssembly 对于 DOM 交互还不完整。这意味着即使在 Yew 中,有时我们也依赖于调用 JavaScript。以下是涉及的库的概述。
wasm-bindgen
wasm-bindgen
是一种在 JavaScript 和 Rust 函数之间建立调用桥梁的库和工具。
web-sys
web-sys
crate 提供 Web API 的绑定,允许我们以 Rust 的方式编写安全的 JavaScript 代码。
示例:
- JS
- RS
let document = window.document
use wasm_bindgen::UnwrapThrowExt;
use web_sys::window;
let document = window()
.expect_throw("window is undefined")
.document()
.expect_throw("document is undefined");