我有一个项目,包括<代码>Tauri后附和Yew
前言。 I m试图节省一个表格变量,以存档tauri_plugin_store/code>。 但文件是可怕的,我并不肯定如何打上<条码>。 由我前言或仅从<代码>save_to_file
功能中产生的变量,以便能够节省数据。 这就是我的Tauri <代码>main.rs。 like
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use tauri::Error;
use tauri_plugin_store::StoreBuilder;
use serde_json::json;
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greets(name: &str) -> String {
format!("Hello, {}! You ve been greeted from Rust!", name)
}
#[tauri::command]
fn save_to_file(day: &str, text: &str, parent: &str) -> Result<(), Error>{
println!(">>>>{}", text);
return Ok(())
}
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![greets, save_to_file])
.plugin(tauri_plugin_store::Builder::default().build())
.setup(|app| {
let mut store = StoreBuilder::new(app.handle(), "/Users/a/Desktop/tauri_project/store.bin".parse()?).build();
Ok(store.insert("a".to_string(), json!("b"))?)
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
As you can see I have save_to_file()
to saving my data. I m invoking this function from my form.rs
inside of Yew app. I m able to call it and pass the data, but no save it, because store
is defined in main
as local variable
fn execute_effect(user_record: Record) {
spawn_local(async move {
if user_record.day == "" || user_record.text == "" || user_record.parent == "" {
return;
} else {
let args = to_value(&Record {
day: user_record.day.clone(),
text: user_record.text.clone(),
parent: user_record.parent.clone(),
})
.unwrap();
let new_msg = invoke("save_to_file", args).await.as_string();
log!("{:#?}", new_msg);
}
});
}
是否有这方面的工作?
我看到了这个例子。
//As you may have noticed, the Store crated above isn t accessible to the frontend. To interoperate with stores created by JS use the exported with_store method:
use tauri::Wry;
use tauri_plugin_store::with_store;
let stores = app.state::<StoreCollection<Wry>>();
let path = PathBuf::from("path/to/the/storefile");
with_store(app_handle, stores, path, |store| store.insert("a".to_string(), json!("b")))
但是,这真的说不是什么? 如何从<代码>进口app
?