英文:
cannot find function `start_app` in crate `yew`
问题
以下是您要翻译的内容:
我正在尝试使用Yew编写简单的Rust Web编程,这是main.rs的源代码:
```rust
use lat12::App;
fn main() {
yew::start_app::<App>();
}
这是一个简单的库:
use yew::prelude::*;
#[function_component(App)]
pub fn app() -> Html {
html! {
<h1>{"欢迎使用Yew进行Rust编程"}</h1>
}
}
这是cargo.toml:
[package]
name = "lat12"
version = "0.1.0"
edition = "2021"
# 更多键和它们的定义请参见 https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
yew = "0.20.0"
如果我将yew的版本更改为:"0.19.3",它可以正常运行,但如果我使用最新版本("0.20.0"),它会出现错误消息:"在crate yew
中找不到函数 start_app
",我应该怎么办?
<details>
<summary>英文:</summary>
I'm trying to write a simple Rust web programming with Yew, this is main.rs source code :
use lat12::App;
fn main() {
yew::start_app::<App>();
}
and this is a simple library :
use yew::prelude::*;
#[function_component(App)]
pub fn app() -> Html {
html! {
<h1>{"Welcome to Rust in Yew using library"}</h1>
}
}
and this is cargo.toml :
[package]
name = "lat12"
version = "0.1.0"
edition = "2021"
See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
yew = "0.20.0"
If I change version of yew to : "0.19.3" it can run smoothly, but if I use newest version ("0.20.0") it can't run with error message : "cannot find function `start_app` in crate `yew`",..... what should I do ?
</details>
# 答案1
**得分**: 3
从[0.19.0到0.20.0迁移指南](https://github.com/yewstack/yew/blob/03cd37f1eab039f47b7f919ff1df59fff5b9f01c/website/docs/migration-guides/yew/from-0_19_0-to-0_20_0.mdx):
> ## Yew 渲染器
> `start_app*` 已被替换为 `yew::Renderer`。
>
> 您需要启用 `csr` 特性来使用 `yew::Renderer`。
<details>
<summary>英文:</summary>
From the [0.19.0 to 0.20.0 migration guide](https://github.com/yewstack/yew/blob/03cd37f1eab039f47b7f919ff1df59fff5b9f01c/website/docs/migration-guides/yew/from-0_19_0-to-0_20_0.mdx):
> ## Yew Renderer
> `start_app*` has been replaced by `yew::Renderer`.
>
> You need to enable feature `csr` to use `yew::Renderer`.
</details>
# 答案2
**得分**: 2
将以下内容添加到您的Cargo.toml文件中:
```toml
[dependencies]
yew = { version = "0.20.0", features = ["csr"] }
使用以下代码将您的应用挂载到自定义根元素:
#[wasm_bindgen]
/// 在给定的根HTML元素上初始化并启动应用
pub fn init_app(
root: web_sys::Element,
) {
yew::Renderer::<App>::with_root(
root,
)
.render();
}
您的HTML页面应该包含一个带有ID的<div>
元素:
<div id="app"></div>
<script type="module">
import init, { init_app } from "./pkg/app.js";
var app_element = document.getElementById("app");
init().then(async () => {
try {
init_app(app_element);
} catch (e) {
console.error(e);
}
});
</script>
请注意,这是一个关于Rust语言和Web开发的代码示例,包括Cargo.toml中的依赖项和在HTML页面中挂载Yew应用的代码。
英文:
Add this to your Cargo.toml:
[dependencies]
yew = { version = "0.20.0", features = ["csr"] }
Use this to mount your App to a custom root element:
#[wasm_bindgen]
/// init and start app on given root html element
pub fn init_app(
root: web_sys::Element,
) {
yew::Renderer::<App>::with_root(
root,
)
.render();
}
Your html page should have a div element with id:
<div id="app" />
<script type="module">
import init, { init_app } from "./pkg/app.js";
var app_element = document.getElementById("app");
init().then(async () => {
try {
init_app(app_element);
} catch (e) {
console.error(e);
}
});
</script>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论