本文介绍了如何使用节点“fs"?在角度 5 内的电子中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to use Electron and Angular5 to write my first desktop App but unfortunately i am stuck in using the fs module. It seems that I have imported fs correctly (no errors within Visual Studio Code and code completion) but when i tried using "fs.readFile" the console prints out this error:

Uncaught TypeError: __WEBPACK_IMPORTED_MODULE_2_fs__.readFile is not a function

这是我目前服务的代码:

This is the code of my service so far:

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 没有加载或什么?感谢大家的帮助!

Do I miss something here? Seems that fs is not loaded or something? Thanks for your help everyone!

推荐答案

在kimy82的评论帮助下找到了答案!

I found the answer with the help of the comments from kimy82!

首先我需要通过简单地使用来获取 Angular5 webpack.config.js:

First i needed to get the Angular5 webpack.config.js by simply using:

ng eject

之后我打开了 webpack.config.js 并添加了以下内容:

After that i opened up the webpack.config.js and added the following:

"target": "node-webkit"

简单的节点"对我来说不起作用,因为电子使用铬,这应该没问题.

Simply "node" did not work out for me and since electron uses a Chromium this should be ok.

谢谢大家!

这篇关于如何使用节点“fs"?在角度 5 内的电子中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 21:42