gpt4 book ai didi

vuex - 未捕获的类型错误 : Vue is not a constructor - Vuex with Vue 3

转载 作者:行者123 更新时间:2023-12-02 19:17:06 33 4
gpt4 key购买 nike

我正在尝试将 Vuex 与 Vue 3 一起使用。这是我在 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');

尝试此操作时,出现以下错误:

Uncaught TypeError: Vue is not a constructor

我尝试使用官方 Vuex 文档中推荐的语法,但这也不起作用。有谁知道如何解决这个问题吗?

最佳答案

出现此错误是因为您正在像 Vuex 3 那样调用 new Vuex.Store

如果您使用Vue 3,则必须通过以下命令安装Vuex 4:

# npm
npm install --save vuex@next

# yarn
yarn add vuex@next

然后构建您的商店并将其“注入(inject)”到 Vue 实例,如下所示:

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

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

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

app.use(store);

请参阅 Vuex 文档以获取更多详细信息:

Vuex 4 for Vue 3

关于vuex - 未捕获的类型错误 : Vue is not a constructor - Vuex with Vue 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63560503/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com