【问题标题】:How fix __dirname not defined when using electron events with Vue?在 Vue 中使用电子事件时如何修复未定义的 __dirname?
【发布时间】:2020-10-27 20:42:41
【问题描述】:

我使用nklayman/vue-cli-plugin-electron-builder 创建了一个使用 Vue/Vuex 准备的电子应用程序。它附带文件main.jsbackground.js,包括 Vue 组件起点。但我无法让这些事件发挥作用。我在下面的尝试在渲染时产生Uncaught ReferenceError: __dirname is not defined(编译很好)。

组件:Splash.vue

<template>
    <div @click="open">open</div>      
</template>

<script>

const { ipcMain } = require('electron')

export default {
    methods: {
        open()
        {
            ipcMain.on('my-open-event', (event, arg) => {
                console.log(event, arg)
            })
        }
    }
}
</script>

background.js

import { app, protocol, BrowserWindow } from 'electron'

...

app.on('my-open-event', async () => {
    try {
        "Will call some executable here";
    } catch (e) {
        console.error(e)
    }
})

main.js

import Vue from 'vue'
import App from './App.vue'
import store from './store'

Vue.config.productionTip = false

new Vue({
  store,
  render: h => h(App)
}).$mount('#app')

完全错误:

Uncaught ReferenceError: __dirname is not defined
    at eval (webpack-internal:///./node_modules/electron/index.js:4)
    at Object../node_modules/electron/index.js (chunk-vendors.js:1035)
    at __webpack_require__ (app.js:849)
    at fn (app.js:151)
    at eval (webpack-internal:///./node_modules/cache-loader/dist/cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/components/Splash.vue?vue&type=script&lang=js&:6)
    at Module../node_modules/cache-loader/dist/cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/components/Splash.vue?vue&type=script&lang=js& (app.js:986)
    at __webpack_require__ (app.js:849)
    at fn (app.js:151)
    at eval (webpack-internal:///./src/components/Splash.vue?vue&type=script&lang=js&:2)
    at Module../src/components/Splash.vue?vue&type=script&lang=js& (app.js:1271)

任何想法我做错了什么?

【问题讨论】:

  • 我在几乎完全相同的设置上遇到了完全相同的错误:在我的情况下,它是一个 Vue-Electron-Vuetify 设置,我已经修改了 vue.config.js:我会把它粘贴进去在这里,但是太长了。所以我已经将我的更改推送到我在 GitHub 上的仓库:github.com/SethEden/electronVueJS-App

标签: vue.js electron vuex


【解决方案1】:

为了解决这个问题,我在项目根目录中创建了一个文件vue.config.js,其中包含内容

module.exports = {
    pluginOptions: {
        electronBuilder: {
            nodeIntegration: true
        }
    }
}

【讨论】:

  • 我确认此解决方案完美运行。但是警告因为这个解决方案打开了一个电子安全问题在这里解释:electronjs.org/docs/tutorial/…
  • 它工作正常,谢谢... 节点:v14.17.1 @vue/cli 4.5.13 Electron 13.1.2 Vuetify v2
  • 如何在 vitejs 中进行这项工作?
【解决方案2】:

electron中有两个进程,主进程和renderer进程。您的 Vue 组件是渲染器进程。为了在这两个进程之间进行通信,您需要进程间通信。因此,在您的情况下,您可能会使用ipcMainbackground.js 中定义一个频道。你会写这样的东西:

ipcMain.on("my-custom-channel", (event, args) => handleEvent(event, args));

然后在你的 Vue 组件中,你会使用渲染器进程,ipcRendere,例如:

import { ipcRenderer } from "electron";

export default {
    methods: {
        open() {
            ipcRenderer.send('my-custom-channel', "hello from Vue!")
        }
    }
}

要点是:您不能将app.on 用于您的自定义事件。 app.on 处理预定义的电子事件。请改用ipcMain.on

参考:https://www.electronjs.org/docs/api/ipc-main

【讨论】:

    【解决方案3】:

    您可以应用这篇文章中描述的解决方案 How to import ipcRenderer in vue.js ? __dirname is not defined

    这样就可以从vue文件中调用window.ipcRenderer.send(channel, args...)。

    只要确保你在 vue.config.js 上配置了 preload.js:

    // vue.config.js - project root
    module.exports = {
      pluginOptions: {
        electronBuilder: {
           preload: 'src/preload.js'  //make sure you have this line added
        }
      }
    }
    

    另一个解决方案可以在这里找到https://medium.com/swlh/how-to-safely-set-up-an-electron-app-with-vue-and-webpack-556fb491b83,它使用 __static 来引用预加载文件,而不是在 vue.config.js 上配置它。要使其工作,您可以在 BrowserWindow 构造函数中禁用预加载 es-lint 警告:

    // eslint-disable-next-line no-undef
    preload: path.resolve(__static, 'preload.js')
    

    并确保您在 /public 文件夹中添加了 preload.js 文件

    【讨论】:

      猜你喜欢
      • 2020-09-30
      • 2021-08-03
      • 2021-07-01
      • 2021-03-05
      • 2017-07-02
      • 2020-01-03
      • 2020-12-16
      • 2021-08-29
      • 1970-01-01
      相关资源
      最近更新 更多