再次调用相同的onclick函数会产生错误



再次调用相同的函数会导致move错误。

use gloo::console::log;
use yew::prelude::*;
use web_sys::HtmlInputElement;
use wasm_bindgen::JsCast;
#[function_component(CheckMnemonic)]
pub fn check_mnemonic() -> Html {
let tags_state: UseStateHandle<Vec<String>> = use_state(||vec![]);
let tags_state_clone1 = tags_state.clone();
let tags_state_clone2 = tags_state.clone();
let remove_tag = |index: usize| {
let mut tags: Vec<_> = tags_state_clone1.to_vec();
tags.remove(index);
tags_state_clone1.set(tags);
};
let add_tag =  Callback::from(move |event: MouseEvent| {
let data = event
.target()
.unwrap()
.unchecked_into::<HtmlInputElement>()
.value();
let mut tags: Vec<_> = tags_state_clone2.to_vec();
tags.push(data.trim().to_owned());
tags_state_clone2.set(tags.clone());
log!(format!("{:?}", tags));
});
let countries = vec!["India", "Norway", "Bhutan"];
html! {
<>
{for tags_state.iter().map(|v| {
html! {
<p> {v} </p>
}
})}
{ for countries.iter().map(|cont| {
html!{
<input type="text" class="form-control" readonly=true value={*cont} onclick={add_tag}/> // New code: Sending props
}
})}



</>
}
}

错误:

error[E0507]: cannot move out of `add_tag`, a captured variable in an `FnMut` closure
--> src/components/accounts/multistep_account_creation/check_mnemonic.rs:45:94
|
21 |     let add_tag =  Callback::from(move |event: MouseEvent| {
|         ------- captured outer variable
...
43 |         { for countries.iter().map(|cont| {
|                                    ------ captured by this `FnMut` closure
44 |             html!{
45 |                 <input type="text" class="form-control" readonly=true value={*cont} onclick={add_tag}/> // New code: Sending props
|                                                                                              ^^^^^^^ move occurs because `add_tag` has type `yew::Callback<web_sys::MouseEvent>`, which does not implement the `Copy` trait

对不起,add_tag.clone()已经解决了这个问题

<input type="text" class="form-control" readonly=true value={*cont} onclick={add_tag.clone()}/> // New code: Sending props

相关内容

  • 没有找到相关文章

最新更新