Tauri Rust基本示例
命令是在
src-tauri/src/main.rs
文件中定义的。 要创建一个命令,只需添加一个函数,并使用 #[tauri::command]
注释:#[tauri::command]
fn my_custom_command() {
println!("I was invoked from JS!");
}
必须向构建器函数提供一个命令列表,如下所示:
// Also in main.rs
fn main() {
tauri::Builder::default()
// This is where you pass in your commands
.invoke_handler(tauri::generate_handler![my_custom_command])
.run(tauri::generate_context!())
.expect("failed to run app");
}
现在,可以从 JS 代码中调用这个命令:
// When using the Tauri API npm package:
import { invoke } from '@tauri-apps/api/tauri'
// When using the Tauri global script (if not using the npm package)
// Be sure to set `build.withGlobalTauri` in `tauri.conf.json` to true
const invoke = window.__TAURI__.invoke
// Invoke the command
invoke('my_custom_command')