我试图在Yew组件的create函数中检索用户的lat/lon,这样我就可以做一些数学运算,并将有用的决策传递给子组件。我已经让Yew钩子"use_geolocation()“工作得很好,但它只在function_component中运行,而且在其他组件中使用该位置似乎没有任何简单的方法。
然后我找到了这个整洁的教程,它使用wasm_bindgen和Seed:: app ::orders::Orders对应用程序进行“廉价克隆”,并调用Javascript函数。绑定看起来像:
#[wasm_bindgen]
extern "C" {
type GeolocationCoordinates;
#[wasm_bindgen(method, getter)]
fn latitude(this: &GeolocationCoordinates) -> f64;
#[wasm_bindgen(method, getter)]
fn longitude(this: &GeolocationCoordinates) -> f64;
type GeolocationPosition;
#[wasm_bindgen(method, getter)]
fn coords(this: &GeolocationPosition) ->
GeolocationCoordinates;
}以及获取地理位置的函数,将Tor Hovland教程中的代码块拼接在一起:
let (app, msg_mapper) = (orders.clone_app(), orders.msg_mapper());
let geo_callback = move |position: JsValue| {
let pos: GeolocationPosition = position.into();
let coords = pos.coords();
app.update(msg_mapper(Msg::Position(
coords.latitude(),
coords.longitude(),
)));
};
let geolocation = web_sys::window()
.expect("Unable to get browser window.")
.navigator()
.geolocation()
.expect("Unable to get geolocation.");
let geo_callback_function =
Closure::wrap(
Box::new(|pos| geo_callback(pos)) as Box<dyn Fn(JsValue)>
);
geolocation.get_current_position(
&geo_callback_function.as_ref().unchecked_ref()
).expect("Unable to get position");
geo_callback_function.forget();我尝试了这个路由,但发现将行seed = "0.9.1"添加到我的Cargo.toml中会产生编译错误,这与wasm_bindgen中的闭包和seed中的闭包之间的类型不匹配有关。为了完整起见,在此包括:
error[E0283]: type annotations needed for `Closure<T>`
--> /home/djmcmath/.cargo/registry/src/github.com-1ecc6299db9ec823/seed-
0.9.1/src/browser/service/routing.rs:87:9
|
87 | let closure = Closure::new(move |event: web_sys::Event| {
| ^^^^^^^
|
= note: cannot satisfy `_: WasmClosure`
note: required by a bound in `Closure::<T>::new`
--> /home/djmcmath/.cargo/registry/src/github.com-1ecc6299db9ec823/wasm-bindgen-
0.2.81/src/closure.rs:251:17
|
251 | T: ?Sized + WasmClosure,
| ^^^^^^^^^^^ required by this bound in `Closure::<T>::new`
help: consider giving `closure` an explicit type, where the type for type parameter
`T` is specified
|
87 | let closure: Closure<T> = Closure::new(move |event: web_sys::Event| {
| ++++++++++++
help: consider specifying the type argument in the function call
|
87 | let closure = Closure::new::<F>(move |event: web_sys::Event| {
| +++++在把头撞在砖墙上一段时间后,我决定不使用种子,但我不知道还有什么方法可以让这个应用程序的“廉价克隆”来正确地计算出生命周期。否则,我将在geo_callback_function上得到可预测的生存期错误:
let geo_callback_function =
Closure::wrap(
Box::new(|pos: JsValue| geo_callback(pos)) as Box<dyn Fn(JsValue)>
);错误消息:
error[E0597]: `geo_callback` does not live long enough
--> src/ferry_route.rs:213:37
|
213 | Box::new(|pos: JsValue| geo_callback(pos)) as Box<dyn Fn(JsValue)>
| ------------------------^^^^^^^^^^^^------
| | | |
| | | borrowed value does not live long enough
| | value captured here
| cast requires that `geo_callback` is borrowed for `'static`
...
221 | }
| - `geo_callback` dropped here while still borrowed所以我现在不知所措。似乎获取用户的位置比所有这些都要简单。我对任何一条道路都是开放的。(工作的定义:我可以在Yew组件的create函数中获取用户的lat/lon,这样我就可以做一些数学运算,并将有用的决策传递给子组件。)
发布于 2022-06-24 03:49:38
好吧,我想我知道了。或者至少,我有个有用的东西。我不确定这是最优雅的解决方案。基本上,将种子部分从函数中删除,只需从JsValue中拉出和弦,如下所示:
fn geo_callback(position: JsValue) {
let pos = JsCast::unchecked_into::<GeolocationPosition>(position);
let coords = pos.coords();
log::info!(
"Latitude: {}. Longitude: {}.",
coords.latitude(),
coords.longitude()
);
};在这一点上,我非常肯定,我可以采取这些和弦,并做一些有用的东西。
https://stackoverflow.com/questions/72738194
复制相似问题