0

Currently I am working with Vue CLI version ^3.0.0 and Vuex ^3.6.0, but when I want to use Vue.use(Vuex). It says in the browser cannot read use of undefined.

Code:

main.js

import App from './App.vue'
import store from './store'

new Vue({
    store,
    render: h => h(App)
}).$mount('#app')

Store.js:

import Vue from 'vue';
import Vuex from 'Vuex';

Vue.use(Vuex);

export default Vuex.Store({
    state: {
        count: 0
    },
    mutations: {
        increment(state) {
            state.count++
        }
    }
})

enter image description here

Error received:

Uncaught TypeError: Cannot read property 'use' of undefined

Do you guys have any idea?

Thanks in advance.

1
  • If you setup vue with vue-cli you can run: vue add vuex and it'll do all the work Commented Feb 16, 2021 at 14:52

3 Answers 3

1

If you're using Vue 3, it doesn't support global config. You'll need to do it like this I think:

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

const store = createStore(
  // options
);

const app = createApp(
  // options
);

app.use(store);

Also you will need to upgrade to vuex@4 for Vue 3 support.

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

4 Comments

I been looking for Vuex version 4, but didnt see any version of npm install, can you maybe send a link?
Try npm install vuex@next
npm install [email protected]?
I have installed your version, but now it doesn't show any result, but it doesnt give any error
0

Store.js:

import { createStore } from 'vuex'

export default createStore({

state: {...},
mutations: {...},
action: {...},
getters: {...}

})

main.js


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

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

1 Comment

And how can I use those state? Can I use it like ReactJS like this.state.name
0

Move

import Vue from 'vue';
import Vuex from 'Vuex';

to main.js since you need to call before its initialize

Comments

Your Answer

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