在封闭参数中键入不匹配+关闭寿命
在紫杉中,我试图将回调绑定到窗口大小事件,触发 msg :: resize
update。我遇到了:E0631在闭合参数中类型不
s
- 匹配 “ rel =“ nofollow noreferrer”> https://github.com/jamesmcguigan/fractals/blob/blob/bd1fb20e138696fc2af9b121a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a132528336b6b6b6b622 andsrc/src/src/components/compon/compon/conters
use gloo_console::log;
use gloo_events::EventListener;
use yew::prelude::*;
pub struct CanvasQuestion {}
pub enum Msg { Resize }
impl Component for CanvasQuestion {
type Message = Msg;
type Properties = ();
fn create(_ctx: &Context<Self>) -> Self {
Self { }
}
fn view(&self, _ctx: &Context<Self>) -> Html {
html! { <canvas id="canvas"/> }
}
fn rendered(&mut self, ctx: &Context<Self>, first_render: bool) {
if first_render {
// WORKS
ctx.link().send_message(Msg::Resize);
// found signature of `fn(yew::Event)
// let on_window_resize = |_event: Event| { // BROKEN
let on_window_resize = |_event: &Event| { // WORKS
ctx.link().send_message(Msg::Resize);
};
// expected signature of `for<'r> fn(&'r yew::Event)
EventListener::new( &web_sys::window().unwrap(),
"resize", on_window_resize );
}
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::Resize => { true } // rerender
}
}
}
storm
替换 on_window_resize = | _ event:event |
用 on_window_resize = | _event:&amp; event |
修复了以下错误
error[E0631]: type mismatch in closure arguments
--> src/components/canvas_question.rs:68:43
|
64 | let on_window_resize = |_event: Event| {
| --------------- found signature of `fn(yew::Event) -> _`
...
67 | EventListener::new( &web_sys::window().unwrap(),
| ------------------ required by a bound introduced by this call
68 | "resize", on_window_resize );
| ^^^^^^^^^^^^^^^^ expected signature of `for<'r> fn(&'r yew::Event) -> _`
|
note: required by a bound in `EventListener::new`
--> /home/jamie/.cargo/registry/src/github.com-1ecc6299db9ec823/gloo-events-0.1.2/src/lib.rs:338:12
|
338 | F: FnMut(&Event) + 'static,
| ^^^^^^^^^^^^^ required by this bound in `EventListener::new`
,但会暴露出试图访问访问 ctx.link的Rust Lifetime问题()
从闭合内部。
error[E0521]: borrowed data escapes outside of associated function
--> src/components/fractal.rs:70:28
|
51 | fn rendered(&mut self, ctx: &Context<Self>, first_render: bool) {
| --- - let's call the lifetime of this reference `'1`
| |
| `ctx` is a reference that is only valid in the associated function body
...
70 | let listener = EventListener::new( &web_sys::window().unwrap(),
| ____________________________^
71 | | "resize", on_window_resize );
| | ^
| | |
| |___________________________________________________________________________`ctx` escapes the associated function body here
| argument requires that `'1` must outlive `'static`
- 有没有一种方法来定义
ctx的静态寿命:上下文
? -
ctx.link()
触发内部消息传递的最佳方法吗? - 直接调用
self.update(msg :: resize)
直接调用self.update(self> self.update)会更容易吗?
-
ctx.link()
是否需要在canvasquestion结构中存储为self.link
? - 编写和绑定
on_window_resize()
函数的正确方法是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@dogbert 对
| _EVENT的引用的评论| _Event:&amp; event |
filect代码>错误[E0631]:在闭合参数中键入不匹配,butr在最终解决方案中不需要这一点。RTFM发现手册具有我需要的代码模式,我需要
这使用移动
ctx.link()。callback()
按值而不是参考闭合,从而避免了生命的问题。@Dogbert's comment of passing a reference to
|_event: &Event|
fixederror[E0631]: type mismatch in closure arguments
, butr this turned out not to be needed in the final solution.RTFM discovered the manual had the code pattern I needed
This uses the move keyword to pass
ctx.link().callback()
into the closure by value rather than reference, thus avoiding the lifetimes issue.