如何导出分配给variables的需求?

我正在执行以下操作,以使其在<app></app>可见

index.html的:

 <script> var electron = require('electron') </script> <app></app> <script src="bundle.js"></script> 

App.vue:

 const ipc = electron.ipcRenderer console.log(ipc) 

但是,我用ESLint得到un-usedun-defined var错误,所以我决定这样做:

requires.js:

 var electron = require('electron') exports.electron = electron 

index.html的:

  <script src="requires.js"></script> </head> <body> <app></app> <script src="bundle.js"></script> 

但是现在我得到这个错误: requires.js:3 Uncaught ReferenceError: exports is not defined

input和input电子的正确方法是什么?

注意:要求直接在App.vue中使用electron不起作用。 你只能在index.html要求electron

完整的例子: https //github.com/alexcheninfo/vue-electron-simple

你似乎试图做的是把electron定义为一个全局variables; 为了工作,你可以在第一个例子中设置window.electron = require('electron')在你的index.html中。 (它会在你的bundle.js中可用)

但是,使用全局的这种不好的做法,而不是必要的。 你应该做什么,只是在你的代码中使用require 。 你说这不起作用:它不这样做的原因可能是你使用webpack或类似的东西来创buildbundle.js 。 此外,您可能在Node中而不是在Electron中运行绑定过程,因此require('electron')不能按预期工作。 请注意,它在您的index.html中不起作用。

如果你想继续使用这个设置,你可以重命名Electron的require来区分捆绑期间求解的require和运行时parsing的require 。 换句话说, window.electronRequire = requireindex.html的脚本标记中,然后在你的代码中使用electronRequire('electron')

话虽如此,为什么要把所有东西捆绑在一起呢? 电子有完整的节点集成,所以你可以使用常规的节点模块; 这些文件不是通过HTTP发送的,所以把所有东西都捆绑到一个文件中是没有什么好处的。