【问题标题】:electron ipcRenderer from Vue.js Single File Component来自 Vue.js 单文件组件的电子 ipcRenderer
【发布时间】:2019-10-23 05:21:29
【问题描述】:

我正在尝试在我的 Vue 单一文件组件中使用 Electron 的 ipcRenderer 来填充一些数据。用ipcRenderer.send(...) 调用很容易,但在回复中我想用响应消息更新我的组件的每个实例。我认为ipcRenderer.on(...) 中的评论最能解释我的问题。有没有好的方法来做到这一点。我对 JS 完全陌生。

<template>
  <v-container fluid>
    <v-btn @click="do_action()">{{title}}</v-btn>
    <v-textarea v-model="response_message">
    </v-textarea>
  </v-container>
</template>

<script>
  const { ipcRenderer } = require('electron')

  export default {
    props: ['title'],
    data: function(){
      return {
        response_message: "Original Message"
      }
    },
    methods: {
      do_action: function() {
        ipcRenderer.send('cmnd_foo')
      }
    },
  }

  ipcRenderer.on('cmnd_foo-reply', (event, a_new_message) => {
    // obviously this.response_message isn't in scope...
    // how can I get this intent to work
    this.response_message = a_new_message
  })
</script>

【问题讨论】:

  • Something like this, perhaps? - 我对 Electron 不太熟悉,所以对它持保留态度.. 编辑 - 看起来你需要使用/实例化 @987654325 @在你的main.js file...?

标签: javascript vue.js electron vue-component


【解决方案1】:

-- 更新--

使用 vuex 商店或类似的东西。

-- 原创--

我似乎找到了一种方法。也许还有更好的方法?

<template>
  <v-container fluid>
    <v-btn @click="do_action()">{{title}}</v-btn>
    <v-textarea v-model="response_message">
    </v-textarea>
  </v-container>
</template>

<script>
  const { ipcRenderer } = require('electron')

  var catcher = 0;
  function setMessage(msg) {
    this.response_message = msg
  }

  export default {
    props: ['title'],
    data: function(){
      return {
        response_message: "Original Message"
      }
    },
    methods: {
      do_action: function() {
        catcher = setMessage.bind(this)
        ipcRenderer.send('cmnd_foo')
      }
    },
  }

  ipcRenderer.on('cmnd_foo-reply', (event, a_new_message) => {
    catcher(a_new_message);
  })
</script>

【讨论】:

  • 你可以直接在函数中调用bind,如果你在生命周期钩子中注册ipcRenderer,比如mounted mounted: function(){ ipcRenderer.on('event', function (){ do something }.bind(this)) }
  • 找到了与 Antonio 建议的类似的解决方案:memorytin.com/2018/07/26/…
  • 我确实最终放弃了这种做事方法,现在依赖于 vuex 商店。因此,我在 vuex 商店中设置了一些东西,而不是 ipcRenderer.on,并观察了该属性。我不建议你在这个答案中这样做,除非你只是想快速让一些东西工作。
【解决方案2】:

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

只要确保你在 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 文件

【讨论】:

    【解决方案3】:

    只需在末尾添加remote

    const { ipcRenderer } = require('electron').remote
    

    【讨论】:

    • .remote 有什么作用?
    • 它定义了一个在渲染器中工作的电子实例:我不知道如何解释,但是:对于主进程使用:require('electron') 和渲染器使用:require('电子').remote
    猜你喜欢
    • 2018-10-09
    • 2020-10-07
    • 2022-01-25
    • 1970-01-01
    • 2019-02-06
    • 2017-05-28
    • 2021-08-11
    • 2017-11-17
    • 1970-01-01
    相关资源
    最近更新 更多