After upgrading to Vue 3, I noticed that Vue.prototype.$foo
was no longer available. Additionally, I discovered that this._vm
was missing in my version of Vuex.
Following the recommendation from the Vue 3 documentation, I delved into the Provide / Inject method which allowed me to access globals within components but not within the store.
To solve this issue, I opted to utilize globalProperties on the Vue object and standard properties on store
, setting them 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 find advantageous about this approach is the consistent way I can access globals 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 actions, mutations, and getters as well.
Once I implemented this solution, I no longer needed to access the entire Vue instance within the store. However, if necessary, you can assign store.$app = app
; in the same location in main.js
.