通过vue和电子共享全局var

时间:2018-06-07 04:13:20

标签: javascript node.js vue.js electron electron-vue

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

1 个答案:

答案 0 :(得分:0)

我只使用电子(在devdependencies中)和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'});
        },