简体   繁体   中英

Access global vue variable from vuex store module

I hope someone can point me in the right direction here. In my Vue.js application, I have defined a global variable $test in my main.ts

import Vue from 'vue'
import App from './App.vue'
import { store } from '../src/store/store';


Vue.config.productionTip = false

Vue.prototype.$store = store;

Vue.prototype.$test = 'Testing';

new Vue({
    store,
    render: h => h(App)
}).$mount('#app');

How do I access this variable from a Vuex store module? I've tried this.$test and other combinations with no luck.

This is from the vuex store module (shortened for this example). This module is imported to my store.ts as a separate module.

export default {
    state: {
        .....
    },
    mutations: {
        ......
    },
    actions: {
        testMyVar(context: any) {
            console.log(this.$test); //What am I doing wrong here???
        }
    },
    getters: {
        ....
    }
}

Edit : basically what I want to do is to mock a library and initiate it from my index.html on my local dev station. Then make the initiated class available from my store modules.

In production the class is already initiated and the variable just needs to be accessible from my vuex store modules.

Thanks

import Vue from 'vue'

export default {
state: {
    .....
},
mutations: {
    ......
},
actions: {
    testMyVar(context: any) {
        console.log(Vue.prototype.$test); //just import vue in store
    }
},
getters: {
    ....
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM