You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tauri命令执行中途向前端emit事件的实现与架构咨询

Rust + Tauri 模块错误处理与前端事件触发疑问

我正在学习Rust和Tauri,希望实现以下功能:当前端调用任一命令后,Rust端在执行流程中途向前端emit事件,以显示带有消息的模态框。我的初衷是为项目的每个模块单独配置错误处理文件,相关示例代码如下:

main.rs 代码示例

#[tauri::command]
fn test () {
match inner_test(file1, file2, equivalences) {
        Ok(_) => println!("Ok"),
        Err(err) => a_module::errors::report_error(err),
    };
}

a_module/errors.rs 代码示例

// the error enums would be defined here too.
pub fn report_error(error: Anything) {
    // This function should know how to properly treat the different errors this module can "throw"
    match error {
        a_module::errors::ModuleError {
            tauri::emit("showModal", "This is a module that can even use a user configuration to show messages in a different language")
        }
    }
}

项目目录结构

a_module/
├─ errors.rs
├─ main_file.rs
├─ mod.rs
main.rs

现想咨询:

  • 这种代码架构是否属于最佳实践?
  • 至少该需求是否可实现?
  • 我需要在Rust端向前端发送事件,以触发Webview中的操作(如显示提示消息、弹窗等)。从官方文档中了解到,可通过app实例发送事件,但如何在其他模块获取该实例,或设置全局引用以便在其他模块使用?

内容的提问来源于stack exchange,提问作者H3lltronik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 03:52:52