【问题标题】:Run shell script inside Angular app packaged in Electron在 Electron 打包的 Angular 应用程序中运行 shell 脚本
【发布时间】:2018-02-13 02:30:22
【问题描述】:

我有 Angular 2 应用程序打包在 Electron 中。我想知道是否可以从该应用程序运行 shell 脚本。

感谢您的帮助。

【问题讨论】:

标签: angular shell electron


【解决方案1】:

我能够使用 ngx-childprocess

实现这一点

分 3 个步骤:

  1. 在您的电子/角度应用程序中安装 ngx-childprocess

    yarn add ngx-childprocess
    or
    npm install ngx-childprocess --save
    
  2. gx-childprocess 添加到 app.module

    imports: [
        NgxChildProcessModulem
        ....
    
  3. 运行脚本(在本例中我运行的是 java jar)

    import { ChildProcessService } from 'ngx-childprocess';
    ... 
    export class AppComponent {
    
       constructor(private childProcessService: ChildProcessService) {
            console.log('isElectronApp ' + childProcessService.isElectronApp);
            let options: string[] = [];
            childProcessService.childProcess.exec('java -jar child-process-test-1.0.jar', 
                                                  options,
                                                  (data) => {console.log(data);});
       }
    }
    

【讨论】:

  • 您用作 childProcessService.childProcess.exec 函数的第三个参数的回调箭头函数必须指定所有三个“err”、“stdout”和“stderr”(或您选择的任何名称)才能获取第二个脚本的输出。如果您使用单个(数据),就像在编辑之前一样,如果脚本已成功运行,您只会得到 null 。但是我们大多数人都对脚本的输出感兴趣,所以这就是为什么我们必须指定所有三个并考虑/阅读第二个,例如,如果您想在 stdout 中查看脚本的输出。
  • 嗨,有没有办法使用 ngx-childprocess 运行 .bat 文件??
猜你喜欢
  • 2017-05-03
  • 1970-01-01
  • 2018-11-14
  • 2018-07-03
  • 2017-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-29
相关资源
最近更新 更多