0

我一直在使用 electron-vue,当我尝试通过 electron 和 vue 共享一组全局变量时遇到了一些问题。我尝试过 vuex,发现在主进程(电子)中所做的更改没有出现在 chrome 进程中(值不同)。我也尝试过 electron-vue 但它无法触发 vue 的更新。

4

1 回答 1

0

我只是使用电子(在开发中)和vue,vuex(在依赖中)。主 Electron JS 文件(您的 main.js 或 app.js)中不需要任何代码。我喜欢这样:

index.html(主窗口):

<script src="js/index.js"></script>

index.js:

const Vuex = require('vuex');

const store = new Vuex.Store({
    state: {
        // your global variables
        var1: 'test',
        var2: true
        },
    mutations: {
        setVar(state, data) {
            state[data.mykey] = data.val;
            } // setpref
        } // mutations
    }); // store

在任何其他渲染器 js 或组件中:

// no require needed except vue.js done in html

computed: {
    // Variable got from the global store
    var1: function () { 
        return this.$store.state.var1; 
        }
    },

methods: {
    foo: function () {
        // changing global store variable
        this.$store.commit('setVar', {mykey: 'var1', val: 'test2'});
        },
于 2018-06-07T07:19:45.113 回答