Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js 3: Cannot import Vue global object

I'm going crazy trying to reconcile the Vue 3 CLI output into something that works with various tutorials and plugins that all use the Vue object, as in Vue.createApp(.... In my project, I can use

import {createApp} from 'vue';
createApp(...

but import Vue from 'vue'; results in Vue still being undefined. I have Vue 3 installed via NPM. Clearly there is something critical that I don't understand about NPM imports, how could the {createApp} import work if importing the whole module does not work?

From package.json:

"dependencies": {
    "@babel/polyfill": "^7.12.1",
    "apexcharts": "^3.22.1",
    "core-js": "^3.6.5",
    "d3": "^6.2.0",
    "vue": "^3.0.0",
    "vue-router": "^4.0.0-rc.1",
    "vue3-apexcharts": "^1.0.0"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "@vue/compiler-sfc": "^3.0.0",
    "babel-eslint": "^10.1.0",
    "babel-plugin-transform-regenerator": "^6.26.0",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^7.0.0-0"
  },

Here is my temporary main.js. This prints 'undefined' followed by the correct createApp function definition:

import Vue from 'vue';
import {createApp} from 'vue';
console.log(Vue);   
console.log(createApp);   
like image 519
Gregorio246 Avatar asked Jan 25 '23 15:01

Gregorio246


1 Answers

If you're working with CDN Vue is available as global variable which could be used to create instance by calling the method createApp like Vue.createApp({...}), but if you're working with a bundler which uses npm modules, there no Vue object imported from vue module so you should import createApp from it to create a new instance like :

import {createApp} from 'vue';
let app=new createApp({...})
app.use(somePlugin)
app.mount("#app")

for more details please check the migration guide of global API

like image 82
Boussadjra Brahim Avatar answered Feb 13 '23 20:02

Boussadjra Brahim