【发布时间】:2023-02-05 14:00:36
【问题描述】:
我开始用 Yew 学习 Rust。我遵循了文档中的反例,现在我正在尝试实现输入功能。我想在 html 中显示输入值。我能够 console.log 但不能在浏览器中呈现它。
我错过了什么?
这是我到目前为止的代码:
use wasm_bindgen::{JsCast, UnwrapThrowExt};
use web_sys::HtmlInputElement;
use yew::prelude::*;
#[function_component]
fn App() -> Html {
let name = use_state(|| "");
let oninput = Callback::from(move |input_event: InputEvent| {
let name = name.clone();
let target: HtmlInputElement = input_event
.target()
.unwrap_throw()
.dyn_into()
.unwrap_throw();
//web_sys::console::log_1(&target.value().into()); // <- can console the value.
move |_: HtmlInputElement| name.set(&target.value().as_str());
});
html! {
<div>
<input {oninput} />
<p>{"name: "}<h5>{name}</h5></p> // <-- here is the error
</div>
}
}
fn main() {
yew::Renderer::<App>::new().render();
}
但我收到此错误:
error[E0277]: `UseStateHandle<&str>` doesn't implement `std::fmt::Display`
--> src/main.rs:22:29
|
22 | <p>{"name"}<h5>{name}</h5></p>
| ^^^^
| |
| `UseStateHandle<&str>` cannot be formatted with the default formatter
| required by a bound introduced by this call
|
= help: the trait `std::fmt::Display` is not implemented for `UseStateHandle<&str>`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: required for `UseStateHandle<&str>` to implement `ToString`
= note: required for `VNode` to implement `From<UseStateHandle<&str>>`
= note: required for `UseStateHandle<&str>` to implement `Into<VNode>`
= note: 2 redundant requirements hidden
= note: required for `UseStateHandle<&str>` to implement `Into<NodeSeq<UseStateHandle<&str>, VNode>>`
我尝试使用 name.get()、&name、name.clone() 以及它们的组合,但我总是遇到错误。你能解释一下为什么我无法在浏览器中显示值吗?
我感谢任何帮助。
【问题讨论】:
-
请发布来自
cargo check(不是您的 IDE)的完整错误。 -
@ChayimFriedman 我用货物检查的输出更新了错误消息。谢谢。