3

如何将本地转换closurejs_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()));
4

1 回答 1

5

我找到了这个。

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!
于 2020-02-04T13:13:47.833 回答