我正在使用 Tauri 构建 GUI。在我的后端我定义了两个命令:
#[tauri::command]
fn functional_test(msg: String) {
println!("Received : {}", msg);
}
#[tauri::command]
fn no_functional_test(my_msg: String) {
println!("Received : {}", my_msg);
}
fn main() {
tauri::Builder::default()
.setup(...)
.invoke_handler(tauri::generate_handler![functional_test, no_functional_test])
.run(tauri::generate_context!())
.expect("failed to run app");
}
请注意,两个函数之间的唯一区别是参数名称:msg
vs my_msg
。
在前端(使用反应),我有这样的代码块:
export function AComponent({name}: {name: string}) {
useEffect(() => {
const fetchSomething = async () => {
try {
await invoke('functional_test', {msg:"This is a message"})
await invoke('no_functional_test', {my_msg:"This is also a message"})
} catch (error) {
await invoke('functional_test', {msg:error})
}
};
fetchSomething();
}, [name]);
return (...)
}
在这里,我尝试调用这两个函数中的每一个。由于不知道如何显示前端的日志,所以使用工作函数进行调试。
有输出:
Received : This is a message
Received : invalid args `myMsg` for command `no_functional_test`: command no_functional_test missing required key myMsg
显然,结果的第一行是执行了前端代码的第5行,但是第二行是执行了第8行,因为第6行导致了错误。正如我们所看到的,Tauri 希望参数的名称为myMsg
,而不是my_msg
我在后端定义的名称。
怎么了?这是正常的吗?