浏览器 API
2026/5/21大约 1 分钟euvuirustwasmusage-introductionplatform
euv 框架基于 web-sys 和 wasm-bindgen 提供浏览器 API 访问能力。所有类型通过 use euv::* 即可使用,无需单独引入 web-sys 或 js-sys。
Window 和 Document
use euv::*;
let win: Window = window().expect("no global window exists");
let doc: Document = win.document().expect("should have a document");导航与路由
let win: Window = window().expect("no global window exists");
let location: Location = win.location();
// 获取当前 hash 路由
let hash: String = location.hash().unwrap_or_default();
// 设置 hash 路由(页面导航)
let _ = location.set_hash("#/about");本地存储
let win: Window = window().expect("no global window exists");
let storage: Option<Storage> = win.local_storage().unwrap_or_default();
if let Some(storage) = storage {
// 写入
let _ = storage.set_item("key", "value");
// 读取
let value: Option<String> = storage.get_item("key").unwrap_or_default();
// 删除
let _ = storage.remove_item("key");
}异步操作
使用 spawn_local(euv 重新导出的 wasm_bindgen_futures::spawn_local)在 WASM 中执行异步任务:
spawn_local(async move {
// 异步操作,如 fetch 请求
// 完成后通过信号更新 UI
});提示
所有浏览器 API 通过 web-sys crate 访问,euv 在 Cargo.toml 中已预启用常用 web-sys 特性(如 Window、Document、Storage、Clipboard、Navigator、FileReader、FileList、IntersectionObserver 等),并通过 use euv::* 统一导出,无需手动添加 web-sys 依赖。