2

I'm trying to use Vuex with Vue 3. Here is my code in main.js:

import { createApp } from 'vue';
import App from './App.vue';
import Vuex from 'vuex';

const app = createApp(App);
app.use(Vuex);

const store = new Vuex.Store({
    state: {
        counter: 0
    }
});

app.store(store);
app.mount('#app');

When trying this, I get the following error:

Uncaught TypeError: Vue is not a constructor

I have tried using the syntax recommended in the official Vuex docs but this does not work either. Does anyone know how to fix this?

5
  • What are your exact versions of Vue and Vuex? Commented Aug 24, 2020 at 12:00
  • Looking at my package.json they are vue ^3.0.0-0 and vuex ^3.5.1 Commented Aug 24, 2020 at 12:10
  • 1
    The package.json is not a reliable way to check the exact installed version. That said, it looks like you have Vuex 3 when you need Vuex 4. See github.com/vuejs/vue-next#vuex Commented Aug 24, 2020 at 12:15
  • vue 3.0.0-rc.7 specifically Commented Aug 24, 2020 at 12:17
  • 1
    This fixed my issue, thank you Commented Aug 24, 2020 at 12:25

1 Answer 1

5

This error occurs because you are calling new Vuex.Store as is done for Vuex 3.

If you are using Vue 3, you must install Vuex 4 by issuing:

# npm
npm install --save vuex@next

# yarn
yarn add vuex@next

Then build your store and "inject" it to the Vue instance as follows:

import { createStore } from 'vuex';
import { createApp } from 'vue';

const store = createStore({
  state () {
    return {
      count: 1
    }
  }
})

const app = createApp({ /* your root component */ });

app.use(store);

Refer to Vuex docs for more details:

Vuex 4 for Vue 3

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.