rust `从js部分调用简单的tauri函数时,使用此命令前必须调用.manage()`

pgccezyw  于 2023-03-30  发布在  其他
关注(0)|答案(1)|浏览(168)

我目前正在使用Tauri React/typescript应用程序,在使用Tauri函数时遇到错误。当我调用函数init_project时,程序崩溃并出现以下错误:

thread 'main' panicked at 'state not managed for field `state` on command `init_project`. You must call .manage() before using this command', /home/korocouille/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tauri-1.2.4/src/state.rs:51:7
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace

下面是Taruri函数:

#[tauri::command]
pub fn init_project(state: tauri::State<InternalStudioState>, root_folder: &str) -> Result<StudioState, APIError> {
    Ok(StudioState::sample())
}

下面是这个函数的调用:

export async function invokeInitProject(path: string): Promise<StudioState> {
  const res = await invoke<StudioState>("init_project", {
    root_folder: path
  }).catch((err) => {
    throw err;
  });
  console.log(res);
  return res;
}

和主要的:

fn main() {
    tauri::Builder::default()
        .manage(InternalStudioState::new())
        .invoke_handler(tauri::generate_handler![
            api::commands::studio_state,
            api::commands::load_project,
            api::commands::load_project_with_view,
            api::commands::init_project,
            api::commands::init_project_with_view,
            api::commands::up_to_date,
            api::commands::root_folder,
            api::commands::reload_project,
            api::commands::view_kind,
            api::commands::current_view,
            api::commands::switch_view,
            api::commands::project_name,
            api::commands::set_project_name,
            ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Tauri部分实际上很简单,init_project所做的唯一事情就是返回一个预定义的示例,所以我不知道为什么会发生这个错误。

rdlzhqv9

rdlzhqv91#

问题来自主文件中的导入。我只是将此use ireflex_studio::api::{types::{ProjectViewRootNode, StudioState}, commands::InternalStudioState};删除到此use ireflex_studio::api::commands;

相关问题