Tauri Rust基本示例

2024-01-26 09:59 更新
命令是在 src-tauri/src/main.rs 文件中定義的。 要創(chuàng)建一個命令,只需添加一個函數(shù),并使用 #[tauri::command] 注釋:
#[tauri::command]
fn my_custom_command() {
  println!("I was invoked from JS!");
}

必須向構(gòu)建器函數(shù)提供一個命令列表,如下所示:

// 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");
}

現(xiàn)在,可以從 JS 代碼中調(diào)用這個命令:

// 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')


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號