使用 web-sys
将 web-sys
添加为 Cargo.toml
的依赖项
[dependencies]
wasm-bindgen = "0.2"
[dependencies.web-sys]
version = "0.3"
features = [
]
为你要使用的 API 启用 cargo features
为了保持构建速度非常快,web-sys
将每个 Web 接口都放在一个 cargo feature 后面。在 API 文档中查找您要使用的类型或方法;它将列出必须启用才能访问该 API 的 features。
例如,如果我们正在寻找 window.resizeTo
函数,我们将在 API 文档中搜索 resizeTo
。我们会找到 web_sys::Window::resize_to
函数,它需要 Window
feature。为了访问该函数,我们在 Cargo.toml
中启用 Window
feature。
[dependencies.web-sys]
version = "0.3"
features = [
"Window"
]
调用该方法!
# #![allow(unused_variables)] #fn main() { use wasm_bindgen::prelude::*; use web_sys::Window; #[wasm_bindgen] pub fn make_the_window_small() { // Resize the window to 500px by 500px. let window = web_sys::window().unwrap(); window.resize_to(500, 500) .expect("could not resize the window"); } #}