某天你想使用Vuex.Store(),然后就可能遇上如下麻烦
当你用Vue.use(Vuex)就会报如下错误:
Uncaught TypeError: Cannot read property ‘use’ of undefined
当你不要Vue.use(Vuex)又会报如下错误:
Uncaught Error: [vuex] must call Vue.use(Vuex) before creating a store instance.
习惯了vue2的Vue.use(),在vue3当中就会感到有点蛋疼,因为它更喜欢像createApp()这种导出被暴露的方法去实例化引用。
npm uninstall vuex
npm i vuex@4.0.0-alpha.1
import { createApp } from 'vue'
import { store } from './store/store'
import App from './App.vue'
const app = createApp(App)
app.use(store)
app.mount('#app')
import { createStore } from 'vuex'
// Create a new store instance.
const store = createStore({
state () {
return {
patients: []
}
}
})
export default store;
我们正在将vuex 3语法与Vue 3一起使用,而vue 3和具有以上语法的vuex 4互相兼容。