Tauri从Rust调用JavaScript

https://tauri.app/v1/guides/features/events/
https://github.com/tauri-apps/tauri/discussions/3018

main.rs
use tauri::Manager;

#[tauri::command]
fn eventtest(name: &str, app_handle: tauri::AppHandle) -> String {
    let msg = format!("Hello, {}! You've been greeted from Rust!", &name);
    app_handle.emit_all("event_test", &msg).unwrap();
    println!("{}", msg);
    format!("{}", msg)
}

fn main() {
    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![eventtest])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

index.html
<script>
 const taurievent = window.__TAURI__.event
  taurievent.listen("event_test",function(event){
   var payload = event.payload;
   window.header.innerHTML = payload;
 });
 setTimeout(function() {
  invoke('eventtest', { name: 'frontend' }).then((response) => {
  //window.header.innerHTML = response;
 });
 }, 100);
</script>

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

17 + 7 =