[Vuejs]-Sharing a global var through vue and electron

0๐Ÿ‘

I just use electron (in devdependencies) and vue, vuex (in dependencies). No code is needed in the main Electron JS file (your main.js or app.js). I do like this :

index.html (main window) :

<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

in any other renderer js or component :

// 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'});
        },

Leave a comment