Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I access $data on Vue 3 instance?

I've got a strange problem: I'm unable to access data() from a vue instance:

const vueapp = Vue.createApp({
  data(){
    return {
      form:{ .... }
[etc..]

// later:

console.log(vueapp.$data, vueapp.form) // both undefined

Why?

like image 357
Luca Reghellin Avatar asked Sep 13 '25 22:09

Luca Reghellin


1 Answers

https://v3.vuejs.org/guide/instance.html#creating-an-application-instance

The app and its root component are subtly but importantly different. The options you pass to createApp don't exist on the app, but its root component.

The options passed to createApp are used to configure the root component. That component is used as the starting point for rendering when we mount the application.

const app = Vue.createApp(RootComponent)
const vm = app.mount('#app')

You can do vm.$data and vm.form in the above code sample, but not app.$data or app.form.

like image 170
ceejayoz Avatar answered Sep 16 '25 12:09

ceejayoz