#[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 参数的属性时遇到错误。每当我引用 props 结构中的一个属性时,我都会收到一条错误消息,指出它“转义了函数体”。我想知道是否有办法解决这个问题。我将结构拆分为其变量,但不能在 html 中使用它们!宏或回调。由于同样的错误,我也无法将 itemIndex: &i32 转换为 i32 以进行回调。