Tauri + Yew 项目教程
1. 项目的目录结构及介绍
tauri-yew-demo/
├── src/
│ ├── main.rs
│ └── ...
├── src-tauri/
│ ├── Cargo.toml
│ ├── tauri.conf.json
│ └── ...
├── frontend/
│ ├── index.html
│ ├── main.rs
│ └── ...
├── .gitignore
├── README.md
└── ...
src/
: 包含 Rust 后端代码的主要目录。src-tauri/
: 包含 Tauri 配置文件和相关资源。frontend/
: 包含 Yew 前端代码和相关资源。.gitignore
: 指定 Git 忽略的文件和目录。README.md
: 项目说明文档。
2. 项目的启动文件介绍
后端启动文件
src/main.rs
是 Rust 后端的入口文件,负责启动 Tauri 应用。
fn main() {
tauri::Builder::default()
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
前端启动文件
frontend/main.rs
是 Yew 前端的入口文件,负责渲染前端页面。
use yew::prelude::*;
struct App;
impl Component for App {
type Message = ();
type Properties = ();
fn create(_ctx: &Context<Self>) -> Self {
Self
}
fn view(&self, _ctx: &Context<Self>) -> Html {
html! {
<div>
<h2 class=["heading"]>{"Hello World"}</h2>
</div>
}
}
}
fn main() {
yew::start_app::<App>();
}
3. 项目的配置文件介绍
Tauri 配置文件
src-tauri/tauri.conf.json
是 Tauri 的主要配置文件,包含应用的名称、版本、窗口设置等。
{
"build": {
"distDir": "../dist",
"devPath": "http://localhost:8080",
"beforeDevCommand": "npm run serve",
"beforeBuildCommand": "npm run build"
},
"tauri": {
"bundle": {
"active": true,
"targets": "all",
"identifier": "com.example.app",
"icon": ["icon.png"],
"resources": [],
"externalBin": [],
"copyright": "",
"category": "DeveloperTool",
"shortDescription": "",
"longDescription": "",
"deb": {
"depends": []
},
"macOS": {
"frameworks": [],
"minimumSystemVersion": "",
"exceptionDomain": "",
"signingIdentity": null,
"entitlements": null
},
"windows": {
"certificateThumbprint": null,
"digestAlgorithm": "sha256",
"timestampUrl": ""
}
},
"allowlist": {
"all": true
},
"windows": [
{
"title": "Tauri App",
"width": 800,
"height": 600,
"resizable": true,
"fullscreen": false
}
],
"security": {
"csp": "default-src 'self'"
}
}
}
Cargo 配置文件
src-tauri/Cargo.toml
是 Rust 项目的配置文件,包含依赖项、包信息等。
[package]
name = "tauri-yew-demo"
version = "0.1.0"
edition = "2018"
[dependencies]
tauri = { version = "1.0", features = ["api-all"] }
yew = "0.19"
以上是 tauri-yew-demo
项目的基本教程,涵盖了目录结构、启动文件