Create Yew App 使用教程

Create Yew App 使用教程

create-yew-appSet up a modern Yew web app by running one command. 项目地址:https://gitcode.com/gh_mirrors/cr/create-yew-app

1、项目的目录结构及介绍

Create Yew App 生成的项目目录结构如下:

my-app
├── README.md
├── .gitignore
├── Cargo.toml
├── index.html
├── LICENSE-APACHE
├── LICENSE-MIT
├── tailwind.config.js
├── tailwind.css
├── Trunk.toml
├── public
│   ├── favicon.ico
│   ├── logo.svg
└── src
    ├── lib.rs
    ├── main.rs
    ├── app
    │   ├── about.rs
    │   ├── home.rs
    │   └── mod.rs
    └── components
        ├── nav.rs
        └── mod.rs

目录结构介绍

  • README.md: 项目说明文件。
  • .gitignore: Git 忽略文件配置。
  • Cargo.toml: Rust 项目的依赖和元数据配置文件。
  • index.html: 项目的主 HTML 文件。
  • LICENSE-APACHELICENSE-MIT: 项目许可证文件。
  • tailwind.config.js: Tailwind CSS 配置文件。
  • tailwind.css: Tailwind CSS 样式文件。
  • Trunk.toml: Trunk 构建工具的配置文件。
  • public: 静态资源文件夹,包含 favicon 和 logo。
  • src: 源代码文件夹,包含 Rust 代码。
    • lib.rs: 库模块文件。
    • main.rs: 主程序入口文件。
    • app: 应用模块文件夹,包含 about 和 home 页面。
    • components: 组件模块文件夹,包含导航组件。

2、项目的启动文件介绍

项目的启动文件是 src/main.rs,它是 Rust 程序的入口点。以下是 main.rs 的基本结构:

fn main() {
    yew::start_app::<app::App>();
}

启动文件介绍

  • main 函数: 程序的入口点,调用 yew::start_app 启动 Yew 应用。
  • app::App: 应用的主组件,定义在 src/app/mod.rs 中。

3、项目的配置文件介绍

项目中有几个重要的配置文件:

Cargo.toml

Cargo.toml 是 Rust 项目的依赖和元数据配置文件,包含项目名称、版本、依赖等信息。

[package]
name = "my-app"
version = "0.1.0"
edition = "2018"

[dependencies]
yew = "0.18"

Trunk.toml

Trunk.toml 是 Trunk 构建工具的配置文件,用于配置构建和开发服务器。

[build]
target = "index.html"

[dev]
open = true

tailwind.config.js

tailwind.config.js 是 Tailwind CSS 的配置文件,用于自定义 Tailwind 的配置。

module.exports = {
  purge: [],
  darkMode: false,
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}

以上是 Create Yew App 项目的基本使用教程,涵盖了项目的目录结构、启动文件和配置文件的介绍。

create-yew-appSet up a modern Yew web app by running one command. 项目地址:https://gitcode.com/gh_mirrors/cr/create-yew-app

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 Rust 中使用 Yew 框架编写 Function Component 非常简单。Function Component 是一种无状态组件,只接收 props 参数并返回一个 Virtual DOM 树。 下面是一个简单的例子,演示如何编写一个 Function Component: ```rust use yew::prelude::*; fn function_component(props: &Props) -> Html { html! { <div> <h1>{ props.title }</h1> <p>{ props.content }</p> </div> } } #[derive(Clone, PartialEq, Properties)] struct Props { title: String, content: String, } ``` 在这个例子中,我们定义了一个名为 `function_component` 的函数组件,并接受一个 `props` 参数。在函数组件中,我们使用 `html!` 宏来创建 Virtual DOM 树,并将 `props` 中的 `title` 和 `content` 属性设置为标题和段落的文本内容。 为了使用这个组件,我们需要在父组件中将 `Props` 传递给 `function_component` 函数,并将其渲染到页面上: ```rust use yew::prelude::*; struct App {} impl Component for App { type Message = (); type Properties = (); fn create(_: Self::Properties, _: ComponentLink<Self>) -> Self { App {} } fn update(&mut self, _: Self::Message) -> ShouldRender { false } fn view(&self) -> Html { let props = Props { title: String::from("Hello, World!"), content: String::from("This is a Yew Function Component."), }; function_component(&props) } } ``` 在这个例子中,我们创建了一个名为 `App` 的组件,并在 `view` 方法中将 `Props` 传递给 `function_component` 函数。在实际应用中,我们可以根据需要设置不同的 `Props` 属性来渲染不同的页面内容。 希望这个例子对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

解杏茜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值