|
| 1 | +--- |
| 2 | +title: 从 Rust 调用前端 |
| 3 | +--- |
| 4 | + |
| 5 | +`@tauri-apps/api` NPM 包提供 API 来监听全局事件和特定于 webview 的事件。 |
| 6 | + |
| 7 | +- 监听全局事件 |
| 8 | + |
| 9 | + ```ts |
| 10 | + import { listen } from '@tauri-apps/api/event'; |
| 11 | + |
| 12 | + type DownloadStarted = { |
| 13 | + url: string; |
| 14 | + downloadId: number; |
| 15 | + contentLength: number; |
| 16 | + }; |
| 17 | + |
| 18 | + listen<DownloadStarted>('download-started', (event) => { |
| 19 | + console.log( |
| 20 | + `downloading ${event.payload.contentLength} bytes from ${event.payload.url}` |
| 21 | + ); |
| 22 | + }); |
| 23 | + ``` |
| 24 | + |
| 25 | +- 监听特定的 webview 事件 |
| 26 | + |
| 27 | + ```ts |
| 28 | + import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow'; |
| 29 | + |
| 30 | + const appWebview = getCurrentWebviewWindow(); |
| 31 | + appWebview.listen<string>('logged-in', (event) => { |
| 32 | + localStorage.setItem('session-token', event.payload); |
| 33 | + }); |
| 34 | + ``` |
| 35 | + |
| 36 | +`listen` 函数会在应用程序的整个生命周期内保持事件监听器的注册。 |
| 37 | +要停止监听某个事件,可以使用 `listen` 函数返回的 `unlisten` 函数: |
| 38 | + |
| 39 | +```js |
| 40 | +import { listen } from '@tauri-apps/api/event'; |
| 41 | + |
| 42 | +const unlisten = await listen('download-started', (event) => {}); |
| 43 | +unlisten(); |
| 44 | +``` |
| 45 | + |
| 46 | +:::note |
| 47 | +当执行上下文超出范围时(例如卸载组件时),请始终使用 unlisten 函数。 |
| 48 | + |
| 49 | +当页面重新加载或导航至其他 URL 时,监听器将自动注销。但这不适用于单页应用(SPA)路由器。 |
| 50 | +::: |
| 51 | + |
| 52 | +此外, Tauri 还提供了一个实用函数,用于精确监听一次事件: |
| 53 | + |
| 54 | +```js |
| 55 | +import { once } from '@tauri-apps/api/event'; |
| 56 | +import { getCurrentWebviewWindow } from '@tauri-apps/api/webviewWindow'; |
| 57 | + |
| 58 | +once('ready', (event) => {}); |
| 59 | + |
| 60 | +const appWebview = getCurrentWebviewWindow(); |
| 61 | +appWebview.once('ready', () => {}); |
| 62 | +``` |
| 63 | + |
| 64 | +:::note |
| 65 | +前端发出的事件也会触发这些 API 注册的监听器。更多信息请参阅 [从前端调用 Rust] 文档。 |
| 66 | +::: |
| 67 | + |
| 68 | +#### 监听 Rust 上的事件 |
| 69 | + |
| 70 | +全局事件和特定于 webview 的事件也会传递给在 Rust 中注册的监听器。 |
| 71 | + |
| 72 | +- 监听全局事件 |
| 73 | + |
| 74 | + ```rust title="src-tauri/src/lib.rs" |
| 75 | + use tauri::Listener; |
| 76 | + |
| 77 | + #[cfg_attr(mobile, tauri::mobile_entry_point)] |
| 78 | + pub fn run() { |
| 79 | + tauri::Builder::default() |
| 80 | + .setup(|app| { |
| 81 | + app.listen("download-started", |event| { |
| 82 | + if let Ok(payload) = serde_json::from_str::<DownloadStarted>(&event.payload()) { |
| 83 | + println!("downloading {}", payload.url); |
| 84 | + } |
| 85 | + }); |
| 86 | + Ok(()) |
| 87 | + }) |
| 88 | + .run(tauri::generate_context!()) |
| 89 | + .expect("error while running tauri application"); |
| 90 | + } |
| 91 | + ``` |
| 92 | + |
| 93 | +- 监听特定的 webview 事件 |
| 94 | + |
| 95 | + ```rust title="src-tauri/src/lib.rs" |
| 96 | + use tauri::{Listener, Manager}; |
| 97 | + |
| 98 | + #[cfg_attr(mobile, tauri::mobile_entry_point)] |
| 99 | + pub fn run() { |
| 100 | + tauri::Builder::default() |
| 101 | + .setup(|app| { |
| 102 | + let webview = app.get_webview_window("main").unwrap(); |
| 103 | + webview.listen("logged-in", |event| { |
| 104 | + let session_token = event.data; |
| 105 | + // save token.. |
| 106 | + }); |
| 107 | + Ok(()) |
| 108 | + }) |
| 109 | + .run(tauri::generate_context!()) |
| 110 | + .expect("error while running tauri application"); |
| 111 | + } |
| 112 | + ``` |
| 113 | + |
| 114 | +`listen` 函数会在应用程序的整个生命周期内保持事件监听器的注册。 |
| 115 | +要停止监听某个事件,可以使用 `unlisten` 函数: |
| 116 | + |
| 117 | +```rust |
| 118 | +// 在事件处理程序作用域外取消监听: |
| 119 | +let event_id = app.listen("download-started", |event| {}); |
| 120 | +app.unlisten(event_id); |
| 121 | + |
| 122 | +// 当满足某些事件条件时取消监听: |
| 123 | +let handle = app.handle().clone(); |
| 124 | +app.listen("status-changed", |event| { |
| 125 | + if event.data == "ready" { |
| 126 | + handle.unlisten(event.id); |
| 127 | + } |
| 128 | +}); |
| 129 | +``` |
| 130 | + |
| 131 | +此外, Tauri 还提供了一个实用函数,用于精确监听一次事件: |
| 132 | + |
| 133 | +```rust |
| 134 | +app.once("ready", |event| { |
| 135 | + println!("app is ready"); |
| 136 | +}); |
| 137 | +``` |
| 138 | + |
| 139 | +在这种情况下,事件监听器在第一次触发后立即取消注册。 |
| 140 | + |
| 141 | +[从前端调用 Rust]: /develop/calling-rust/ |
0 commit comments