我有一个 BeginTestView.vue 页面,其中有一个开始测试按钮。单击此按钮将启动一个 exe 文件,其中包含 C# 中的 Nunit 测试。我想在 Vue 组件中使用 child_process execFile 运行 exe。我收到错误模块未找到:错误:无法解析“子进程”:...... 但是,如果您在启动应用程序时从主进程运行此 exe 文件,则一切正常。我如何在vuejs本身中使用child_process?
我尝试在 vue 组件中使用 child_process 运行 exe 文件,但收到错误 Module not find: Error: Can't resolve 'child_process' in 'C:.....
示例代码:
<script setup>
//import { execFile } from 'child_process'
function beginTest(){
const execFile = require('child_process')
execFile("./testexe/TestConsole.exe", (error, stdout, stderr) => {
if (error) {
console.error(`Error executing the file: ${error.message} dirname:${__dirname}`);
return;
}
console.log(`File executed successfully: ${stdout}`);
});
}
</script>
<template>
<div class="content">
<button class="button" @click="beginTest">Begin test</button>
</div>
</template>
<style scoped lang="scss">
...
</style>
错误: 在此输入图片描述
请注意,即使您使用 Electron,Vue 也在客户端(也称为浏览器)运行。您无法在浏览器环境中使用
child_process
。
您可以使用 IPC 让 Node.js 进程 (
main.js
) 执行此操作。