I'm using Vue 3 and Vue.prototype.$foo
seems to have been removed for this version. I also found that in my version of VueX there is no this._vm
.
I explored the Provide / Inject method which is recommended by the Vue 3 docs. This worked nicely for accessing globals from within my components, but I couldn't access them from within store.
The solution I went for was to use globalProperties on the Vue object and standard properties on store
, and set them just before mounting the app.
main.js
:
import store from './store/index';
import App from './App.vue';
// Load custom globals
import conf from '@/inc/myapp.config';
const app = createApp(App)
.use(store);
// Register globals in app and store
app.config.globalProperties.$conf = conf;
store.$conf = conf;
app.mount('#app');
What I like about this is that I can access the globals in the same way in both store and components.
In a component:
export default {
data() {
return {
};
},
created() {
console.log( this.$conf.API_URL );
},
}
...and you can access this.$conf.API_URL
in the same way from actions, mutations and getters.
Once I'd found this solution I no longer needed access to the whole Vue instance from within store, but if you need it for some reason you can assign store.$app = app
; in the same place in main.js
.
this
in vuex does not refer to vue instance that's why it doesn't work, trythis._vm
. – Braccithis._vm
, in myactions.js
filethis
is undefined. – Ebullitionthis
will point to theVuex.Store
instance in which your mutations or actions are in. But this isn't a likely scenario in a real world app. Your code would be a mess and virtually impossible to maintain when you have 200 actions and mutations in one file. So in the simplest of apps then yes this will work, but not really in a decent sized app. – Ebullition