Skip to content

Commit d60cfe9

Browse files
KikkiZdreyfus92
andauthored
i18n(zh-cn): translate /develop/calling-rust.mdx (#3324)
Co-authored-by: Paul Valladares <[email protected]>
1 parent 995c59e commit d60cfe9

File tree

4 files changed

+1114
-3
lines changed

4 files changed

+1114
-3
lines changed

src/content/docs/develop/calling-rust.mdx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Command names must be unique.
3636
Commands defined in the `lib.rs` file cannot be marked as `pub` due to a limitation in the glue code generation.
3737
You will see an error like this if you mark it as a public function:
3838

39-
````
39+
```
4040
error[E0255]: the name `__cmd__command_name` is defined multiple times
4141
--> src/lib.rs:28:8
4242
|
@@ -47,6 +47,7 @@ error[E0255]: the name `__cmd__command_name` is defined multiple times
4747
|
4848
= note: `__cmd__command_name` must be defined only once in the macro namespace of this module
4949
```
50+
5051
:::
5152

5253
You will have to provide a list of your commands to the builder function like so:
@@ -59,7 +60,7 @@ pub fn run() {
5960
.run(tauri::generate_context!())
6061
.expect("error while running tauri application");
6162
}
62-
````
63+
```
6364

6465
Now, you can invoke the command from your JavaScript code:
6566

@@ -236,7 +237,7 @@ invoke('login', { user: 'tauri', password: '0j4rijw8=' })
236237

237238
As mentioned above, everything returned from commands must implement [`serde::Serialize`], including errors.
238239
This can be problematic if you're working with error types from Rust's std library or external crates as most error types do not implement it.
239-
In simple scenarios you can use `map_err` to convert these errors to `String`s:
240+
In simple scenarios you can use `map_err` to convert these errors to `String`:
240241

241242
```rust
242243
#[tauri::command]
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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

Comments
 (0)