0

I'm confused about the difference between the CLI version of Vue.js and the normal version where you use a Vue istance.

On the initial build, the App.js file looks something like this:

import { createApp } from 'vue'
import { App } from './App.vue'

createApp(App).mount('#app')

and the app works fine.

But if I try to change it slightly and use:

import { Vue } from 'vue'
import { App } from './App.vue'

Vue.createApp(App).mount('#app')

all I get is a blank page.

I've been wanting to add a prototype to the Vue instance to access a global variable, but I don't see how if you can't use the Vue class normally.

What's the difference in functionality on the vue-cli when using Vue.createApp and createApp?

Zack Plauché
  • 3,307
  • 4
  • 18
  • 34
  • @BoussadjraBrahim I see that my app does work when using `createApp` vs `Vue.createApp`, but I don't understand *why* it works. – Zack Plauché Aug 24 '21 at 09:01
  • 1
    the first syntax is the correct one because there's no `import { Vue } from 'vue'` in vue 3, and `Vue.createApp` is valid only when using vue 3 via CDN – Boussadjra Brahim Aug 24 '21 at 09:12

1 Answers1

1

Since you're using the vue cli, the first syntax is correct because there's no Vue object imported from vue, for more explanation check this answer. To add a global variable try to use app.config.globalProperties.$globalVar :

import { createApp } from 'vue'
import { App } from './App.vue'

let app=createApp(App)
app.config.globalProperties.$globalVar=555;
app.mount('#app')
Boussadjra Brahim
  • 82,684
  • 19
  • 144
  • 164