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

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

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&#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>();
}

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 : &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 ?

</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):
&gt; ## Yew Renderer
&gt; `start_app*` has been replaced by `yew::Renderer`.
&gt; 
&gt; 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 = &quot;0.20.0&quot;, features = [&quot;csr&quot;] }

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::&lt;App&gt;::with_root(
        root,
    )
    .render();
}

Your html page should have a div element with id:

&lt;div id=&quot;app&quot; /&gt;
&lt;script type=&quot;module&quot;&gt;
  import init, { init_app } from &quot;./pkg/app.js&quot;;
  var app_element = document.getElementById(&quot;app&quot;);
  init().then(async () =&gt; {
    try {
      init_app(app_element);
    } catch (e) {
      console.error(e);
    }
  });
&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:

确定