无法在 crate `yew` 中找到 `start_app` 函数。

huangapple go评论90阅读模式
英文:

cannot find function `start_app` in crate `yew`

问题

以下是您要翻译的内容:

  1. 我正在尝试使用Yew编写简单的Rust Web编程,这是main.rs的源代码
  2. ```rust
  3. use lat12::App;
  4. fn main() {
  5. yew::start_app::<App>();
  6. }

这是一个简单的库:

  1. use yew::prelude::*;
  2. #[function_component(App)]
  3. pub fn app() -> Html {
  4. html! {
  5. <h1>{"欢迎使用Yew进行Rust编程"}</h1>
  6. }
  7. }

这是cargo.toml:

  1. [package]
  2. name = "lat12"
  3. version = "0.1.0"
  4. edition = "2021"
  5. # 更多键和它们的定义请参见 https://doc.rust-lang.org/cargo/reference/manifest.html
  6. [dependencies]
  7. yew = "0.20.0"

如果我将yew的版本更改为:"0.19.3",它可以正常运行,但如果我使用最新版本("0.20.0"),它会出现错误消息:"在crate yew 中找不到函数 start_app",我应该怎么办?

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;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>();
}

  1. 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>
}
}

  1. 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"

  1. If I change version of yew to : &quot;0.19.3&quot; it can run smoothly, but if I use newest version (&quot;0.20.0&quot;) it can&#39;t run with error message : &quot;cannot find function `start_app` in crate `yew`&quot;,..... what should I do ?
  2. </details>
  3. # 答案1
  4. **得分**: 3
  5. 从[0.19.00.20.0迁移指南](https://github.com/yewstack/yew/blob/03cd37f1eab039f47b7f919ff1df59fff5b9f01c/website/docs/migration-guides/yew/from-0_19_0-to-0_20_0.mdx):
  6. > ## Yew 渲染器
  7. > `start_app*` 已被替换为 `yew::Renderer`
  8. >
  9. > 您需要启用 `csr` 特性来使用 `yew::Renderer`
  10. <details>
  11. <summary>英文:</summary>
  12. 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):
  13. &gt; ## Yew Renderer
  14. &gt; `start_app*` has been replaced by `yew::Renderer`.
  15. &gt;
  16. &gt; You need to enable feature `csr` to use `yew::Renderer`.
  17. </details>
  18. # 答案2
  19. **得分**: 2
  20. 将以下内容添加到您的Cargo.toml文件中:
  21. ```toml
  22. [dependencies]
  23. yew = { version = "0.20.0", features = ["csr"] }

使用以下代码将您的应用挂载到自定义根元素:

  1. #[wasm_bindgen]
  2. /// 在给定的根HTML元素上初始化并启动应用
  3. pub fn init_app(
  4. root: web_sys::Element,
  5. ) {
  6. yew::Renderer::<App>::with_root(
  7. root,
  8. )
  9. .render();
  10. }

您的HTML页面应该包含一个带有ID的<div>元素:

  1. <div id="app"></div>
  2. <script type="module">
  3. import init, { init_app } from "./pkg/app.js";
  4. var app_element = document.getElementById("app");
  5. init().then(async () => {
  6. try {
  7. init_app(app_element);
  8. } catch (e) {
  9. console.error(e);
  10. }
  11. });
  12. </script>

请注意,这是一个关于Rust语言和Web开发的代码示例,包括Cargo.toml中的依赖项和在HTML页面中挂载Yew应用的代码。

英文:

Add this to your Cargo.toml:

  1. [dependencies]
  2. yew = { version = &quot;0.20.0&quot;, features = [&quot;csr&quot;] }

Use this to mount your App to a custom root element:

  1. #[wasm_bindgen]
  2. /// init and start app on given root html element
  3. pub fn init_app(
  4. root: web_sys::Element,
  5. ) {
  6. yew::Renderer::&lt;App&gt;::with_root(
  7. root,
  8. )
  9. .render();
  10. }

Your html page should have a div element with id:

  1. &lt;div id=&quot;app&quot; /&gt;
  2. &lt;script type=&quot;module&quot;&gt;
  3. import init, { init_app } from &quot;./pkg/app.js&quot;;
  4. var app_element = document.getElementById(&quot;app&quot;);
  5. init().then(async () =&gt; {
  6. try {
  7. init_app(app_element);
  8. } catch (e) {
  9. console.error(e);
  10. }
  11. });
  12. &lt;/script&gt;

huangapple
  • 本文由 发表于 2023年3月1日 14:30:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75600235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定