首页 > 解决方案 > 如何在 Angular 8 中使用 nodejs 模块?

问题描述

我不能在我的 Angular 8 应用程序中使用 nodeJS 全局模块。例如“fs”模块。我想通过使用 nodeJS 'fs' 模块来执行 writeFile 和 readFile 操作。我尝试了很多方法,但无法在我的 Angular 8 应用程序中访问这些模块。请帮助我...

标签: node.jsangulartypescript

解决方案


如果您正在使用 Angular 开发 Electron 应用程序,则必须进行一些工作来访问节点 api。

  1. 安装ngx-electron并输入 NgxElectronModule(按照Installation自述文件中的说明进行操作)。

  2. 在需要调用 node api 的地方注入 ElectronService。

  3. 通过 . 导入您需要的包this._electronService.remote.require(string)

D:\这是通过节点 api获取文件列表的示例。

const fs = this.electronService.remote.require('fs');
console.log(fs.readdirSync('D:\\'));

推荐阅读