I'm trying to use beforeCreate in main.js. What is the equivalence of this code in Vue 3?
new Vue({
el: '#app',
router,
components: { App },
store: store,
beforeCreate() { this.$store.commit('initialiseStore');},
template: '<App/>'
})
beforeCreate()still exists in Vue 3's Options API.For that code snippet, the only differences in Vue 3 would be:
The creation of the app instance is now done via
Vue.createApp().Note: In the following example, we're using
extends: Apphere so that we could add thebeforeCreate()hook. Otherwise, we could simply docreateApp(App)instead.The Vue plugins are installed off of the app instance's
use()method.The mounting is done via the app instance's
mount()method.demo
Side note: Since you're migrating to Vue 3, Pinia is now the officially recommended state management library that replaces Vuex (now deprecated). See Pinia's migration guide.