【问题标题】:Getting a Uncaught TypeError: Cannot read property 'use' of undefined on Vue.use(Vuex)获取未捕获的类型错误:无法读取 Vue.use(Vuex) 上未定义的属性“使用”
【发布时间】:2020-12-26 03:57:15
【问题描述】:

在我的 vue 前端,我尝试设置 Vuex。在我的商店页面上(在商店目录内,文件名 index.js)。我使用 Vue 3.0 当我运行代码时,我得到无法读取 Vue.use(Vuex) 行上未定义的属性“使用”

这是代码块

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:[]
  },...

【问题讨论】:

    标签: vue.js vuex vuejs3


    【解决方案1】:

    你需要使用Vuex 4 来兼容 Vue 3,而 Vuex 4 引入了一些重大更改,其中之一是如何初始化 store。基本上不再使用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

    【讨论】:

    • 你能否给我一个关于如何更新到 vuex 4 的提示...我将 package.json 中的依赖项更改为 vuex 4.0.0 但控制台返回此版本不存在跨度>
    • 您可以通过npm i --save vuex@nextnpm i --save vuex@4.0.0-beta.4 安装它,这是当前的测试版。
    猜你喜欢
    • 2019-02-26
    • 2022-01-12
    • 2021-12-22
    • 1970-01-01
    • 2021-12-25
    • 2017-07-26
    • 2015-01-06
    相关资源
    最近更新 更多