Appearance
Command 开发
Command 是 Rust 后端暴露给前端的 IPC 接口,每个 Command 对应一个前端可调用的函数。
定义 Command
rust
#[tauri::command]
pub fn greet(name: &str) -> Result<String, CommandError> {
Ok(format!("你好, {}!", name))
}注册 Command
在 lib.rs 中使用 generate_handler! 宏注册:
rust
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![
commands::system::greet,
commands::config::get_all_config,
// ...更多命令
])已内置的 Commands
| 命令 | 模块 | 说明 |
|---|---|---|
greet | system | 问候测试 |
get_system_info | system | 获取系统信息 |
get_all_config | config | 查询所有配置 |
get_config | config | 查询单个配置 |
set_config | config | 设置配置 |
delete_config | config | 删除配置 |
新增 Command 步骤
- 在
commands/目录编写函数,标记#[tauri::command] - 返回
Result<T, CommandError> - 在
lib.rs的generate_handler!中注册 - 在
capabilities/default.json中添加权限(如需要) - 在前端
src/lib/api/编写调用封装