Getting a Uncaught TypeError: Cannot read property 'use' of undefined on Vue.use(Vuex)

Getting a Uncaught TypeError: Cannot read property 'use' of undefined on Vue.use(Vuex)

在我的 vue 前端中,我尝试设置 Vuex。在我的商店页面上(在商店目录内,文件名 index.js)。我使用的是 Vue 3.0 当我 运行 代码时,我得到 Cannot read 属性 'use' of undefined on Vue.use(Vuex) line

这是代码块

import Vue from "vue";
import Vuex from "vuex";
import Api from "../services/api";

Vue.use(Vuex);//here i get the error

export default new Vuex.Store({
  state:{
  articles:[]
  },...

您需要使用 Vuex 4 才能与 Vue 3 兼容,Vuex 4 引入了一些重大更改,其中之一是如何初始化商店。基本上不再使用 new Vuex.Store,您现在使用 createStore 来创建商店对象,并且不再需要在 store.js.

中使用 Vue.use(Vuex)
import { createStore } from 'vuex';

export const store = createStore({
  state: {...}
  // other stuff
})

并且在您的 main.js 文件中:

import { createApp } from 'vue';
import { store } from 'path/to/store.js';

const app = createApp({...})
app.use(store);

您还可以看到 full Vuex 4 example on github