node.js - 如何在 Angular 5 内的 Electron 中使用 Node "fs"

标签 node.js angular typescript electron

我尝试使用 Electron 和 Angular5 编写我的第一个桌面应用程序,但不幸的是我一直在使用 fs 模块。似乎我已经正确导入了 fs(Visual Studio Code 和代码完成中没有错误)但是当我尝试使用“fs.readFile”时,控制台打印出这个错误:

Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_2_fs__.readFile is not a function

到目前为止,这是我的服务代码:

import { Injectable } from '@angular/core';
import { ElectronService } from 'ngx-electron';
import * as fs from 'fs';
import { OpenDialogOptions } from 'electron';

@Injectable()
export class FileService {

  dialog = this._electronService.remote.dialog;
  window = this._electronService.remote.getCurrentWindow();

  constructor(private _electronService: ElectronService) { }

  loadFileContent(): void{
    this.dialog.showOpenDialog(this.window, {},(fileNames) => {
      if(fileNames === undefined){
        console.error("no files selected!");
        return;
      }

      fs.readFile(fileNames[0], "utf-8", (err, data) => {
        if(err){
          console.error("Cannot read file ",err);
          return;
        }

        console.log("The content of the file is : ");
        console.log(data);
      });

    });
  }
}

我在这里错过了什么吗?似乎 fs 没有加载或什么的?感谢大家的帮助!

最佳答案

您还可以使用 remote.require 从 ngx-electron 加载 native Node 模块。

fs;

constructor(private _electronService: ElectronService) { 
    this.fs = this._electronService.remote.require('fs');
}

关于node.js - 如何在 Angular 5 内的 Electron 中使用 Node "fs",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47883109/

相关文章:

javascript - 解析服务器更新现有的用户字段?

node.js - 获取用户 ID 后,Firebase 函数 onCreate 方法无法在 Firestore 上运行

node.js - 在nodejs中生成子进程来调用npm安装的命令会导致Windows中的ENOENT

typescript - 使用 TypeScript 从 Angular2 中的 http 数据链接 RxJS Observables

javascript - 在另一个守卫解决后,Angular2 运行守卫

unit-testing - Angular 2 单元测试 - 多个类实例

node.js - 即使将编译器选项用作angular 7中的目标es6和es2017,我如何解决编译时错误?

javascript - 如何使用 Typescript 检查 React 中基于类的组件的返回值

node.js - Mongodb聚合:Passing arguments to custom function

javascript - 将 TypeScript 与 Jest 一起使用不会捕获语法错误