[英]Vue.js use method of undefined
目前我正在使用 Vue CLI 版本 ^3.0.0 和 Vuex ^3.6.0,但是當我想使用 Vue.use(Vuex) 時。 它說在瀏覽器中無法讀取未定義的使用。
代碼:
main.js
import App from './App.vue'
import store from './store'
new Vue({
store,
render: h => h(App)
}).$mount('#app')
商店.js:
import Vue from 'vue';
import Vuex from 'Vuex';
Vue.use(Vuex);
export default Vuex.Store({
state: {
count: 0
},
mutations: {
increment(state) {
state.count++
}
}
})
收到錯誤:
未捕獲的類型錯誤:無法讀取未定義的屬性“使用”
你們有什么想法嗎?
提前致謝。
如果您使用的是 Vue 3,它不支持全局配置。 我認為你需要這樣做:
import { createApp } from 'vue';
import { createStore } from 'vuex';
const store = createStore(
// options
);
const app = createApp(
// options
);
app.use(store);
您還需要升級到vuex@4
以獲得 Vue 3 支持。
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')
移動
import Vue from 'vue';
import Vuex from 'Vuex';
到 main.js,因為您需要在初始化之前調用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.