Tauri 快速入门。[Vite | Tauri Apps](https://tauri.app/v1/guides/getting-started/setup/vite/) 本文采用 Rust + vue + vite
前言
Vite | Tauri Apps
本文采用 Rust + vue + vite
开始
Rust
下载安装
修改镜像源 ,创建config文件并填入以下内容(…/user/.cargo/config)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[ source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'tuna'
# replace-with = 'ustc'
[ source.tuna]
#清华镜像源
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
[ source.ustc]
# 中科大镜像源
registry = "https://mirrors.ustc.edu.cn/crates.io-index"
#registry = "git://mirrors.ustc.edu.cn/crates.io-index"
[ net]
git-fetch-with-cli = true
快速初始化Tauri
参考 tauri-apps/create-tauri-app: Rapidly scaffold out a new tauri app project. (github.com)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
cargo install create-tauri-app
cargo create-tauri-app
cd project_directory
# 安装依赖
yarn
# 开发环境
yarn tauri dev
# 生产编译
yarn tauri build
问题
1、VSCode 中 /src-tauri/src/main.rs 爆红
1
2
3
4
5
6
7
# 生产环境编译,需要修改 `/src-tauri/tauri.conf.json`
...
"identifier" : "com.tauri.custom_name" ,
...
# 编译生成dist目录就好了
yarn tauri build
开发
方法调用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 后端
// 不带参数
#[tauri::command]
fn my_custom_command1 () {
println! ( "I was invoked from JS!" );
}
// 带参数
#[tauri::command]
fn my_custom_command2 ( invoke_message : String ) {
println! ( "I was invoked from JS, with this message: {} " , invoke_message );
}
fn main () {
tauri ::Builder ::default ()
// This is where you pass in your commands
. invoke_handler ( tauri ::generate_handler! [ my_custom_command1 , my_custom_command2 ])
. run ( tauri ::generate_context! ())
. expect ( "failed to run app" );
}
1
2
3
4
5
6
7
8
// 前端
import { invoke } from '@tauri-apps/api/tauri'
// 不带参数
invoke ( 'my_custom_command1' )
// 带参数
invoke ( 'my_custom_command2' , { invokeMessage : 'Hello!' })
事件
前后端事件是可以相互触发的。调试时如发现异常,先重启应用。
后端事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 开始全局监听
let id = app . listen_global ( "event-name" , | event | {
println! ( "got event-name with payload {:?} " , event . payload ());
});
// 取消全局监听
app . unlisten ( id );
// 触发全局事件
app . emit_all ( "event-name" , Payload { message : "Tauri is awesome!" . into () }). unwrap ();
---------------------------------------------------------------------------
// 开启窗口监听
let main_window = app . get_window ( "main" ). unwrap ();
let id = main_window . listen ( "event-name" , | event | {
println! ( "got window event-name with payload {:?} " , event . payload ());
});
// 取消窗口监听
main_window . unlisten ( id );
// 触发窗口事件
main_window . emit ( "event-name" , Payload { message : "Tauri is awesome!" . into () }). unwrap ();
前端事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { emit , listen } from '@tauri-apps/api/event'
// 开始全局监听
const unlisten = await listen ( 'event-name' , ( event ) => {
console . log ( "listen event-name!" )
})
// 取消全局监听
unlisten ();
// 触发全局事件
emit ( 'event-name' , {
theMessage : 'send event-name!' ,
})
---------------------------------------------------------------------------
import { appWindow , WebviewWindow } from '@tauri-apps/api/window'
// 开始窗口监听
const unlisten = await appWindow . listen ( "event-name" , ( event ) => {
console . log ( "listen event-name!" )
})
// 取消窗口监听
unlisten ();
// 触发窗口事件:方式一
appWindow . emit ( 'event-name' , { message : 'Tauri is awesome!' })
// 触发窗口事件:方式二
const webview = new WebviewWindow ( 'window' )
webview . emit ( 'event-name' )
详细示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 后端
use tauri ::Manager ;
// the payload type must implement `Serialize` and `Clone`.
#[derive(Clone, serde::Serialize)]
struct Payload {
message : String ,
}
fn main () {
tauri ::Builder ::default ()
. setup ( | app | {
let id = app . listen_global ( "event-name" , | event | {
println! ( "got event-name with payload {:?} " , event . payload ());
});
// app.unlisten(id);
app . emit_all ( "event-name" , Payload { message : "Tauri is awesome!" . into () }). unwrap ();
Ok (())
})
. run ( tauri ::generate_context! ())
. expect ( "failed to run app" );
}
1
2
3
4
5
6
7
8
9
10
11
12
// 前端
import { emit , listen } from '@tauri-apps/api/event'
const unlisten = await listen ( 'event-name' , ( event ) => {
console . log ( 'listen event-name event!' )
})
// unlisten();
emit ( 'event-name' , {
theMessage : 'send event-name event!' ,
})
Licensed under CC BY-NC-SA 4.0