#[derive(PartialEq, Properties)]
pub struct ItemProps {
cost: i128,
purchaseCallback: Callback<i32>,
name: String,
imgUrl: String,
cookiesPerSecond: i128,
itemIndex: i32,
}
#[function_component]
pub fn Item(props: &ItemProps) -> Html {
let ItemProps {cost, purchaseCallback, name, imgUrl, cookiesPerSecond, itemIndex} = props;
let onclick: Callback<MouseEvent> = Callback::from(move |_| {
purchaseCallback.emit(*itemIndex);
});
html! {
<div class="item">
<img src={imgUrl.as_str()} />
<span class="item-name">{name.as_str()}</span>
<span class="item-cost">{cost}</span>
<button {onclick}></button>
</div>
}
}
错误信息:
--> src/main.rs:20:41
|
18 | pub fn Item(props: &ItemProps) -> Html {
| ----- - let's call the lifetime of this reference `'1`
| |
| `props` is a reference that is only valid in the function body
19 | let ItemProps {cost, purchaseCallback, name, imgUrl, cookiesPerSecond, itemIndex} = props;
20 | let onclick: Callback<MouseEvent> = Callback::from(move |_| {
| _________________________________________^
21 | | purchaseCallback.emit(*itemIndex);
22 | | });
| | ^
| | |
| |______`props` escapes the function body here
| argument requires that `'1` must outlive `'static`
我正在使用紫杉网络组装框架在 Rust 中构建一个组件。我遇到了一个错误,当我尝试调用回调函数时它给了我一个错误,因为 props 变量转义了函数体。