JS 與 RS 結合
Yew 核心運作的方式,是將一個可重複使用的 UI 可能需要的所有元件,集中放在同個地方(也就是 rust 檔案中),同時在需要時,仍能存取其底層技術。
時至今日,WebAssembly 在與 DOM 互動時,仍然缺少一些功能。這表示,即使使用 Yew,我們有時仍必須仰賴呼叫 JavaScript。以下說明所涉及函式庫的概況。
wasm-bindgen
wasm-bindgen
是連結 JavaScript 與 Rust 函式之間呼叫的函式庫與工具。
web-sys
web-sys
箱子有提供 Web API 的約束,讓我們能用防鏽且安全的模式撰寫 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");