简体   繁体   English

来自 Vue.js 单文件组件的电子 ipcRenderer

[英]electron ipcRenderer from Vue.js Single File Component

I'm trying to use Electron's ipcRenderer inside my Vue sigle file component to populate some data.我正在尝试在我的 Vue 单文件组件中使用 Electron 的 ipcRenderer 来填充一些数据。 It easy enough to call out with a ipcRenderer.send(...) but on the reply I want to update each instance of my component with the response message.使用ipcRenderer.send(...)调用很容易,但在回复时我想用响应消息更新我的组件的每个实例。 I think the comment in ipcRenderer.on(...) explains my issue the best.我认为ipcRenderer.on(...)的评论最好地解释了我的问题。 Is there a good way to do this.有没有好的方法可以做到这一点。 I'm completely new to JS.我对 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>

-- Update -- - 更新 -

Use a vuex store or something like.使用 vuex 商店或类似的东西。

-- Original -- - 原来的 -

I seem to have found a way to do it.我似乎找到了一种方法来做到这一点。 Maybe there is still a better way?也许还有更好的方法?

 <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>

You can apply the solution described on this post How to import ipcRenderer in vue.js ?您可以应用这篇文章如何在 vue.js 中导入 ipcRenderer 中描述的解决方案 __dirname is not defined __dirname 未定义

Just make sure you configure preload.js on vue.config.js:只要确保你在 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
    }
  }
}

Another solution can be found here https://medium.com/swlh/how-to-safely-set-up-an-electron-app-with-vue-and-webpack-556fb491b83 and it use __static to refer to preload file instead of configure it on vue.config.js.另一个解决方案可以在这里找到https://medium.com/swlh/how-to-safely-set-up-an-electron-app-with-vue-and-webpack-556fb491b83它使用 __static 来引用预加载文件而不是在 vue.config.js 上配置它。 To make it work you can disable preload es-lint warning inside of BrowserWindow constructor:要使其工作,您可以在 BrowserWindow 构造函数中禁用预加载 es-lint 警告:

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

And make sure you added preload.js file on /public folder并确保您在 /public 文件夹中添加了 preload.js 文件

只需在最后添加remote

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM