如何在electron-js中的angular-cli中使用文件系统(fs)

Vla*_* G. 2 node.js electron angular-cli

我已经建立了一个angular-cli项目

(@ Angular / cli:1.0.0-rc.2节点:6.10.0 os:linux x64)

使用electron js(v1.6.2)并且我需要使用文件系统来创建/删除.csv文件和文件夹,但是我不能在角度组件中加入

您如何配置angular-cli能够:从'fs'导入fs?

uns*_*age 5

您不会将Angular-CLI配置为使用NodeJS fs模块。

在电子中,您有两个过程;主要和渲染器。主要流程控制着诸如browserWindow之类的项目,该项目本质上是用户在打开应用程序时看到的“窗口”,并依次为视图加载html文件。在这里,在主要过程中,您将导入fs模块。

在渲染过程中,您将在视图中处理动作,并将其发送到主流程。在这里,您将使用IPC通过事件进行通信以对主要流程进行处理。触发该事件后,渲染过程将接收该事件并将其发送给main。Main会用它做一些事情,然后在桌面上打开一个文件。

我建议使用电子API 演示应用程序来查看清晰的示例。这是使用FS(从演示应用程序)打印为pdf的示例。

此外,这是Ray Villalobos使用React编写的电子应用程序 github示例,它具有一些相似的概念,将向您展示如何在应用程序中集成组件。

渲染过程:

const ipc = require('electron').ipcRenderer

const printPDFBtn = document.getElementById('print-pdf')

printPDFBtn.addEventListener('click', function (event) {
  ipc.send('print-to-pdf')
})

ipc.on('wrote-pdf', function (event, path) {
  const message = `Wrote PDF to: ${path}`
  document.getElementById('pdf-path').innerHTML = message
})
Run Code Online (Sandbox Code Playgroud)

主要过程:

const fs = require('fs')
const os = require('os')
const path = require('path')
const electron = require('electron')
const BrowserWindow = electron.BrowserWindow
const ipc = electron.ipcMain
const shell = electron.shell

ipc.on('print-to-pdf', function (event) {
  const pdfPath = path.join(os.tmpdir(), 'print.pdf')
  const win = BrowserWindow.fromWebContents(event.sender)
  // Use default printing options
  win.webContents.printToPDF({}, function (error, data) {
    if (error) throw error
    fs.writeFile(pdfPath, data, function (error) {
      if (error) {
        throw error
      }
      shell.openExternal('file://' + pdfPath)
      event.sender.send('wrote-pdf', pdfPath)
    })
  })
})
Run Code Online (Sandbox Code Playgroud)