0

有了这个简单的 Vue 页面:

<template>
  <div class="home">
    <HelloWorld msg="Welcome to Your Vue.js App"/>
  </div>
</template>

<script>
import HelloWorld from '@/components/HelloWorld.vue'
import { ipcRenderer } from 'electron'

export default {
  name: 'Home',
  components: {
    HelloWorld
  },
  data() {
    return {
      dato: null
    }
  },
  methods: {
    rendererFunct () {
      //ipcRenderer.on('setting', (event, arg) => {
        //console.log(arg);
      //})
    }
  }
}
</script>

import { ipcRenderer } from 'electron' 的唯一存在会产生错误 __dirname is not defined :

在此处输入图像描述

这个问题是与 webpack 配置有关还是由于其他原因?

这是我的 webpack.config.js :

import 'script-loader!./script.js';
import webpack from 'webpack';

const path = require('path');

const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
  target: ['electron-renderer', 'electron-main', 'electron-preload'],
  pluginOptions: {
    electronBuilder: {
      chainWebpackMainProcess: config => {
        config.resolve.alias.set('jsbi', path.join(__dirname, 'node_modules/jsbi/dist/jsbi-cjs.js'));
      }
    },
  },
};

module.exports = {
  entry: './src/background.js',
  target: 'node',
  output: {
    path: path.join(__dirname, 'build'),
    filename: 'backend.js'
  }
}

module.exports = config => {
  config.target = "electron-renderer";
  return config;
};

module.exports = {
  plugins: [
    new CopyPlugin({
      patterns: [
        { from: 'source', to: 'dest' },
        { from: 'other', to: 'public' },
      ],
      options: {
        concurrency: 100,
      },
    }),
  ],
};

module.exports = {
  module: {
    rules: [
      {
        test: /\.s[ac]ss$/i,
        use: [
          // Creates `style` nodes from JS strings
          'style-loader',
          // Translates CSS into CommonJS
          'css-loader',
          // Compiles Sass to CSS
          'sass-loader',
        ],
      },
    ],
  },
};

const supportedLocales = ['en-US', 'it'];

export default const config = {
  plugins: [
    new webpack.ContextReplacementPlugin(
      /date\-fns[\/\\]/,
      new RegExp(`[/\\\\\](${supportedLocales.join('|')})[/\\\\\]index\.js$`)
    )
  ]
}

这是 vue.config.js :

module.exports = {
  configureWebpack: {
    // Configuration applied to all builds
  },
  pluginOptions: {
    electronBuilder: {
      chainWebpackMainProcess: (config) => {
        // Chain webpack config for electron main process only
      },
      chainWebpackRendererProcess: (config) => {
        config.plugin('define').tap((args) => {
          args[0]['IS_ELECTRON'] = true
          return args
        })
      },
      mainProcessFile: 'src/background.js',
      mainProcessWatch: ['src/preload.js'],
    }
  }
}

module.exports = {
  pluginOptions: {
    electronBuilder: {
      disableMainProcessTypescript: false,
      mainProcessTypeChecking: false
    }
  }
}
  • 电子:版本​​ 9.0.0
  • webpack:版本 4.44.1
  • 系统:操作系统:Linux 5.4 Ubuntu 18.04.4 LTS(仿生海狸) CPU:(8) x64 Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz
  • 二进制文件:节点:14.5.0 - ~/.nvm/versions/node/v14.5.0/bin/node 纱线:1.22.4 - /usr/bin/yarn npm:6.14.5 - ~/.nvm/versions/node /v14.5.0/bin/npm
  • 浏览器:Chrome:84.0.4147.105 Firefox:79.0

期待您的帮助。马可

4

2 回答 2

0

__dirname 是一个 NodeJS 变量,在最近的电子版本中,默认禁用节点集成。打开 BrowserWindow 时,应将以下内容添加到选项中:

webpreferences:{
    nodeIntegration: true
}

但是,强烈反对这样做,因为这会引发安全问题。

这似乎为大多数人解决了这个问题(对我来说很遗憾,我现在得到了下一个错误 fs.existsSync is not a function:)

一个更好的解决方案是将您的捆绑器更改为正确的构建模式。你不应该为节点构建,而是为 web 构建,所以 target:esnext 什么的。

如果某些东西需要节点访问,这应该通过在后台线程或预加载脚本中运行来解决。

于 2020-10-13T18:03:31.270 回答
0

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

这样就可以从 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
    }
  }
}
于 2021-09-03T18:36:26.133 回答