gpt4 book ai didi

angular - 在Electron和Angular中使用SerialPort失败

转载 作者:行者123 更新时间:2023-12-03 12:35:05 25 4
gpt4 key购买 nike

我最近开始尝试Electron和SerialPort,并在混合物中添加Angular(7+)时遇到了一点障碍。

所以这是问题所在:

我运行典型的 Angular CLI命令以生成应用程序。我将 Electron 和 Electron 重建作为dev依赖项添加。然后添加SerialPort作为依赖项。

在我的app.component.ts中-

import { Component, OnInit } from '@angular/core';
import * as SerialPort from 'serialport';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {

title = 'electron-angular-serialport';

ngOnInit() {
SerialPort.list();
}
}

然后,我运行npm命令以启动ng生成过程和 Electron 。
npm run electron-build

并且它达到约92%并因以下错误而死亡:
ERROR in ./node_modules/@serialport/bindings/lib/linux-list.js
Module not found: Error: Can't resolve 'child_process' in '/Users/22arwxpx/Documents/Coding/electron-angular-serialport/node_modules/@serialport/bindings/lib'
ERROR in ./node_modules/@serialport/bindings/lib/unix-write.js
Module not found: Error: Can't resolve 'fs' in '/Users/22arwxpx/Documents/Coding/electron-angular-serialport/node_modules/@serialport/bindings/lib'
ERROR in ./node_modules/@serialport/bindings/lib/unix-read.js
Module not found: Error: Can't resolve 'fs' in '/Users/22arwxpx/Documents/Coding/electron-angular-serialport/node_modules/@serialport/bindings/lib'
ERROR in ./node_modules/bindings/bindings.js
Module not found: Error: Can't resolve 'fs' in '/Users/22arwxpx/Documents/Coding/electron-angular-serialport/node_modules/bindings'
ERROR in ./node_modules/bindings/bindings.js
Module not found: Error: Can't resolve 'path' in '/Users/22arwxpx/Documents/Coding/electron-angular-serialport/node_modules/bindings'
ERROR in ./node_modules/@serialport/parser-byte-length/byte-length.js
Module not found: Error: Can't resolve 'stream' in '/Users/22arwxpx/Documents/Coding/electron-angular-serialport/node_modules/@serialport/parser-byte-length'
ERROR in ./node_modules/@serialport/parser-cctalk/cctalk.js
Module not found: Error: Can't resolve 'stream' in '/Users/22arwxpx/Documents/Coding/electron-angular-serialport/node_modules/@serialport/parser-cctalk'
ERROR in ./node_modules/@serialport/parser-delimiter/delimiter.js
Module not found: Error: Can't resolve 'stream' in '/Users/22arwxpx/Documents/Coding/electron-angular-serialport/node_modules/@serialport/parser-delimiter'
ERROR in ./node_modules/@serialport/parser-ready/ready.js
Module not found: Error: Can't resolve 'stream' in '/Users/22arwxpx/Documents/Coding/electron-angular-serialport/node_modules/@serialport/parser-ready'
ERROR in ./node_modules/@serialport/parser-regex/regex.js
Module not found: Error: Can't resolve 'stream' in '/Users/22arwxpx/Documents/Coding/electron-angular-serialport/node_modules/@serialport/parser-regex'
ERROR in ./node_modules/@serialport/stream/stream.js
Module not found: Error: Can't resolve 'stream' in '/Users/22arwxpx/Documents/Coding/electron-angular-serialport/node_modules/@serialport/stream'
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! electron-angular-serialport@0.0.0 electron-build: `ng build --prod && electron .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the electron-angular-serialport@0.0.0 electron-build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/22arwxpx/.npm/_logs/2018-12-08T03_13_40_398Z-debug.log

我还有其他导入方式吗?

最佳答案

简短答案:

  • 安装@ types/node
    npm install --save-dev @types/node
  • 像这样修改您的tsconfig.json-
    {
    "compileOnSave": false,
    .....
    "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "types": [
    "node"
    ]
    }
    }

  • Take note of types and allowSyntheticDefaultImports key.


  • 将此添加到您的polyfills.ts
    (window as any).global = window;
  • 需要串行端口
    import { Component } from '@angular/core';
    import { } from 'electron';
    import Serialport from 'serialport';

    @Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss']
    })
    export class AppComponent {

    constructor() {
    //check if platform is electron
    let isElectron: boolean = window && window['process'] &&
    window['process'].type;

    if (isElectron) {
    let serialport: typeof Serialport = window['require']('serialport');
    let app: Electron.App = window['require']('electron').remote;
    console.log(serialport, app, window['process']);
    }
    }
    }

  • Note : You do not directly require or import native dependencies in angular. Instead we use window['require'] to require the module in our app. The import statement above is just used to provide for typings information to typescript.



    长答案:

    请参阅我的评论 here

    关于angular - 在Electron和Angular中使用SerialPort失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53679217/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com