【发布时间】: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