我正在努力理解如何在 .vue 文件中正确导入 ipcRenderer。
我放入 /src/background.js 文件:
webPreferences: {
nodeIntegration:false,
contextIsolation: true, // protects against prototype pollution
preload: path.join(__dirname, "../dist_electron/preload.js"),
}
Run Code Online (Sandbox Code Playgroud)
而且,基于https://nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration我放入了 preload.js :
window.ipcRenderer = ipcRenderer
Run Code Online (Sandbox Code Playgroud)
webpack.config.js :
module.exports = {
entry: './src/background.js',
target: 'node',
output: {
path: path.join(__dirname, 'build'),
filename: 'background.js'
}
}
Run Code Online (Sandbox Code Playgroud)
为了方便调试,我创建了一个github repo。你可以从这里 git 克隆 repo:https : //github.com/raphael10-collab/ElectronVueTypeScriptScaffolding.git
执行 yarn -> yarn electron:serve 你会得到正确的页面。
但是当在 /src/views/Home.vue 中激活这一行时:
//从“电子”导入 { ipcRenderer }
你会得到这个错误:
Environment Info:
System:
OS: Linux 5.4 Ubuntu 18.04.5 LTS …Run Code Online (Sandbox Code Playgroud)