From 43fd1bc5360c581cc2406da27b5f894461790406 Mon Sep 17 00:00:00 2001 From: qier222 Date: Mon, 23 Nov 2020 16:44:17 +0800 Subject: [PATCH] fix: bugs --- src/App.vue | 2 +- src/store/index.js | 10 ++++++-- src/store/plugins/broadcast.js | 43 +++++++++++++++++----------------- 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/App.vue b/src/App.vue index 40e7970..b1e0142 100644 --- a/src/App.vue +++ b/src/App.vue @@ -36,7 +36,7 @@ export default { }, data() { return { - isElectron: process.env.IS_ELECTRON, // "true" || undefined + isElectron: process.env.IS_ELECTRON, // true || undefined }; }, created() { diff --git a/src/store/index.js b/src/store/index.js index a934104..f61da3c 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -9,7 +9,7 @@ import { changeAppearance } from "@/utils/common"; import updateApp from "@/utils/updateApp"; import pkg from "../../package.json"; // vuex 自定义插件 -import vuexBroadCast from "./plugins/broadcast"; +import { getBroadcastPlugin } from "./plugins/broadcast"; import saveToLocalStorage from "./plugins/localStorage"; if (localStorage.getItem("appVersion") === null) { @@ -24,11 +24,17 @@ updateApp(); Vue.use(Vuex); +let plugins = [saveToLocalStorage]; +if (process.env.IS_ELECTRON === true) { + let vuexBroadCast = getBroadcastPlugin(); + plugins.push(vuexBroadCast); +} + const options = { state, mutations, actions, - plugins: [saveToLocalStorage, vuexBroadCast], + plugins, }; const store = new Vuex.Store(options); diff --git a/src/store/plugins/broadcast.js b/src/store/plugins/broadcast.js index c2f930b..921cbfa 100644 --- a/src/store/plugins/broadcast.js +++ b/src/store/plugins/broadcast.js @@ -1,22 +1,23 @@ -// const electron = import('electron') -const electron = window.require("electron"); -const ipcRenderer = electron.ipcRenderer; +export function getBroadcastPlugin() { + const electron = window.require("electron"); + const ipcRenderer = electron.ipcRenderer; -export default (store) => { - // 第一行初始化第一次的状态 - ipcRenderer.send("vuex-state", store.state); - store.subscribe( - ( - mutation, - { data = "", settings = "", player = {}, contextMenu = {}, liked = {} } - ) => { - const copyState = { data, settings, player, contextMenu, liked }; - ipcRenderer.send("vuex-state", copyState); - } - ); - store.subscribe((mutation, state) => { - if (mutation.type === "updateData") { - ipcRenderer.send("updateData", state.data); - } - }); -}; + return (store) => { + // 第一行初始化第一次的状态 + ipcRenderer.send("vuex-state", store.state); + store.subscribe( + ( + mutation, + { data = "", settings = "", player = {}, contextMenu = {}, liked = {} } + ) => { + const copyState = { data, settings, player, contextMenu, liked }; + ipcRenderer.send("vuex-state", copyState); + } + ); + store.subscribe((mutation, state) => { + if (mutation.type === "updateData") { + ipcRenderer.send("updateData", state.data); + } + }); + }; +}