【问题标题】:update Vuex store from main proccess electron从主进程电子更新 Vuex 商店
【发布时间】:2018-02-08 14:02:51
【问题描述】:

如何通过从主进程提交内容来更新 vuex 存储?例如:

在主线程中:

import store from ../store

ipc.on('someevent', (event, args) => {
    // do stuff with args
store.commit('update-things')
})

并在渲染器中使用这些修改更新组件。

编辑: 真实代码:

main.js

import store from '../store'
const {ipcMain} = require('electron')
const WebTorrent = require('webtorrent')
const client = new WebTorrent()

ipcMain.on('addMagnet', (event, arg) => {
   client.add(arg, function (torrent) {
   var files = []
    torrent.files.forEach(function (file) {

    files.push({
        title: file.name,
        torrent: torrent.infoHash,
        index: torrent.files.indexOf(file),
        duration: '--:--'
    })
   })
  store.commit('addSongs', files)
})

和存储突变是这样的:

addSongs (state, newSongs) {
    newSongs.forEach(function (song) {
      state.songs.push(song)
    })
}

如果有帮助,store 位于与 main.js 不同的目录中。

使用store的组件是:

**by this component:

     <template>
      <div id="playlist">
        <div
          is="song"
          v-for="song in songs"
          v-bind:title="song.title"
          v-bind:torrent="song.torrent"
          v-bind:index="song.index">
    </div>
  </div>

</template>

<script>
import Song from './SongList/Song'

export default {
  name: 'playlist',
  components: { Song },
  computed: {
    songs () {
      return this.$store.getters.songs
    }
  }
}
</script>

【问题讨论】:

  • 这一切看起来都正确,什么没有按预期工作?
  • 当我提交 update-things html 没有改变时,如果有帮助,我可以更新真实代码。
  • 是的,您需要出示相关代码
  • 我认为这行不通。基本上你要处理两个独立的商店,因为主进程与渲染器不同。

标签: javascript vue.js frontend electron


【解决方案1】:

我知道这是一个老话题,但对于未来的搜索者,您可以使用vuex-electron 从主线程分派到商店。

https://www.npmjs.com/package/vuex-electron

【讨论】:

    猜你喜欢
    • 2017-11-09
    • 2017-06-30
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 2018-08-19
    • 2019-01-25
    • 2020-06-18
    • 2017-08-28
    相关资源
    最近更新 更多