我尝试使用 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/