我用nklayman/vue-cli-plugin-electron-builder创建了一个用Vue/Vuex编写的Electron 应用程序.它附带文件主要的js出身背景js,包括Vue组件起点.但我无法让这些活动正常进行.我下面的try 在渲染时得到Uncaught ReferenceError: __dirname is not defined(编译很好).

组件:飞溅.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>

出身背景js

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

...

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

主要的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)

知道我做错了什么吗?

推荐答案

为了解决这个问题,我在ProjectRoot中创建了一个包含内容的文件vue.config.js

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

Vue.js相关问答推荐

无法在cPanel/SSH上构建 | Laravel + Vue优化

点击屏幕的任何位置都会禁用v-overlay

处理多张卡片中的按钮

vue 计算的 ant watch 不会在嵌套属性更改时触发

两个div元素之间的vue2过渡

如何在 A-La-Carte 系统中使用 vuetify 加载程序时导入 vuetify/lib

VueJS CKeditor5 上传图片

Webpack 你可能需要一个合适的加载器来处理这个文件类型

Vuetify 容器不会填充高度

如何将 Nuxt.js 更新到最新版本

vuex: unknown getter: user

无法访问数据对象的嵌套属性

如何在 Vue.js 应用程序中正确下载 Excel 文件?

Vuetify 数据表不显示数据

如何在 Promise 回调中更新 Vue 应用程序或组件的属性?

Vue 2 转换不起作用

v-if 未在计算(computed)属性上触发

根据nuxt中的url参数动态加载组件

如何在 Vue.js 2 应用程序中模拟 onbeforeunload?

Vue.js 显示空格(换行符)