Home > Back-end >  Why can't I access $data on Vue 3 instance?
Why can't I access $data on Vue 3 instance?

Time:11-07

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?

CodePudding user response:

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.

  • Related