Tauri + Vue 3 + TypeScript 获取 Windows 系统详细信息
要获取 Windows 系统的详细信息,您可以使用 Node.js 的 os 模块来实现。以下是一个使用 Tauri、Vue 3 和 TypeScript 获取 Windows 系统详细信息的示例:
// main.ts
import { createApp } from 'vue';
import App from './App.vue';
// 注册 Tauri 插件
import { invoke } from '@tauri-apps/api/tauri';
// 获取 Windows 系统信息的函数
async function getWindowsSystemInfo() {
try {
const response = await invoke('get_windows_system_info');
return response;
} catch (error) {
console.error(error);
return null;
}
}
createApp(App)
.provide('getWindowsSystemInfo', getWindowsSystemInfo) // 注入获取系统信息的函数
.mount('#app');
<!-- App.vue -->
<template>
<div>
<button @click="fetchSystemInfo">获取系统信息</button>
<pre>{{ systemInfo }}</pre>
</div>
</template>
<script>
import { inject, ref } from 'vue';
export default {
name: 'App',
setup() {
const systemInfo = ref('');
const getWindowsSystemInfo = inject('getWindowsSystemInfo');
const fetchSystemInfo = async () => {
const info = await getWindowsSystemInfo();
systemInfo.value = JSON.stringify(info, null, 2);
};
return {
systemInfo,
fetchSystemInfo
};
}
};
</script>
请注意,上述示例中的 get_windows_system_info 是一个自定义的 Tauri 端点,您需要在 Tauri 项目中的 src-tauri/src/main.rs 文件中添加相应的处理函数。以下是一个处理函数的示例:
#[tauri::command]
fn get_windows_system_info() -> Result<serde_json::Value, String> {
use sysinfo::{System, SystemExt};
let mut system = System::new_all();
system.refresh_all();
let mut info = serde_json::json!({
'hostname': system.get_host_name().unwrap_or_default(),
// 其他您希望获取的系统信息
});
Ok(info)
}
请根据您的实际需求修改处理函数,以获取您所需的系统信息。完成后,重新构建 Tauri 项目,并运行 Vue 应用程序,即可通过点击按钮获取 Windows 系统的详细信息。
原文地址: https://www.cveoy.top/t/topic/o4S1 著作权归作者所有。请勿转载和采集!