Electron使用指南——11添加一个新的信息

1、创建 Store

1.1 编辑 store

编辑 /vue-renderer/src/store/index.js

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    isShowModal: false
  },

  mutations: {
    setModalVisible(state, show) {
      state.isShowModal = show
    }
  },

  actions: {
    setModalVisible({commit}, show) {
      commit('setModalVisible', show)
    }
  }
})

1.2 引入 store

编辑 /vue-renderer/src/main.js

// ...
import store from './store'
// ...

2、显示添加窗口

编辑 /vue-renderer/src/components/Header.vue:

<template>
  <header>
    <button id="show-modal" @click="setModalVisible(true)">+</button>
    // ...
  </header>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  methods: {
    ...mapActions(['setModalVisible'])
  },
}
</script>

3、完善添加模态组件

编辑 /vue-renderer/src/components/Modal.vue

<template>
  <div id="modal" v-show="isShowModal">
    <input type="text" id="url" :disabled="status" v-model="url" placeholder="输入 URL ...">
    <button id="add-item" :class="{disabled: status}" :disabled="status" @click="addItem">{{addButtonText}}</button>
    <button id="close-modal" v-show="!status" @click="setModalVisible(false)">取消</button>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'

export default {
  data() {
    return {
      url: '',
      status: false,
      addButtonText: '添加'
    }
  },

  created() {

    // Listen for new item from main process
    ipcRenderer.on('new-item-success', (e, newItem) => {
      console.log(newItem)

      this.status = false
      this.addButtonText = '添加'
      this.url = ''

      this.setModalVisible(false)
    })
  },

  computed: {
    ...mapState(['isShowModal'])
  },

  methods: {
    ...mapActions(['setModalVisible']),

    addItem() {
      if (this.url !== '') {

        // Send new item url to main process
        ipcRenderer.send('new-item', this.url)

        this.status = true
        this.addButtonText = '添加中...'
      }
    }
  }
}
</script>

4、完善主进程 main.js

编辑 /main.js , 在文件代码中的最外层添加 ipcMain 的 new-item 时间监听,重点是 ipc 通信:

//...

// Modules
const { ipcMain } = require('electron')

// Listen for new item request
ipcMain.on('new-item', (e, itemUrl) => {

  // Get new item and send back to renderer
  setTimeout(() => {
    e.sender.send('new-item-success', 'New item from main process')
  }, 2000)
})

// ...
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue 中创建一个Electron 窗口,可以按照以下步骤进行: 1. 安装 electron: ``` npm install electron --save-dev ``` 2. 在 `package.json` 文件中添加以下脚本: ``` "electron": "electron ." ``` 3. 创建一个的组件来作为 Electron 窗口的内容,例如 `MyApp.vue`: ```html <template> <div> <h1>Hello, Electron!</h1> </div> </template> ``` 4. 在 `main.js` 中创建一个Electron 窗口: ```js const { app, BrowserWindow } = require('electron'); const path = require('path'); let mainWindow; function createWindow() { mainWindow = new BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true, contextIsolation: false, devTools: true } }); mainWindow.loadURL(`file://${path.join(__dirname, '../dist/index.html')}`); mainWindow.on('closed', () => { mainWindow = null; }); } app.on('ready', createWindow); app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit(); } }); app.on('activate', () => { if (mainWindow === null) { createWindow(); } }); ``` 其中,`main.js` 是 Electron 的主进程,创建了一个 `BrowserWindow` 来显示 Vue 组件的内容。 5. 在 `App.vue` 中添加按钮来触发打开窗口的事件: ```html <template> <div> <h1>Hello, Vue!</h1> <button @click="openWindow">Open Electron Window</button> </div> </template> <script> export default { methods: { openWindow() { const { remote } = require('electron'); const win = new remote.BrowserWindow({ width: 800, height: 600, webPreferences: { nodeIntegration: true, contextIsolation: false, devTools: true } }); win.loadURL(`file://${__dirname}/index.html#/myapp`); } } }; </script> ``` 在 `openWindow` 方法中,使用 `remote` 模块来创建一个Electron 窗口,并加载 Vue 组件的 URL。 最后,将 `MyApp.vue` 添加到路由中,就可以在打开的 Electron 窗口中看到该组件的内容了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值