如何将本地closure
转换为js_sys::Funtion
?
我想做这样的事情:
let canvas = document.get_element_by_id("canvas").unwrap();
let e: web_sys::HtmlElement = canvas.dyn_into().unwrap();
let f = || {};
e.set_onresize(Some(&f.into()));
答案 0 :(得分:1)
我发现了这个。
https://rustwasm.github.io/wasm-bindgen/reference/passing-rust-closures-to-js.html
就像:
let f = Closure::wrap(Box::new(move || { /* whatever */}) as Box<dyn FnMut()>);
e.set_onresize(Some(f.as_ref().unchecked_ref()));
f.forget(); // It is not good practice, just for simplification!