要获取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系统的详细信息

tauri + vue3 +Typescript 获取Windows系统详细信息

原文地址: https://www.cveoy.top/t/topic/hB5s 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录