基本使用
npm i vuex
一、创建 store 目录
import Vuex from 'vuex'
const store = new Vuex.Store({
state() {
return {
count: 0
}
},
mutations: {
increment(state) {
state.count++
}
}
})
export default store
二、引入 store
import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import store from './store/index'
import router from './router'
const app = createApp(App)
app.use(router)
app.use(ElementPlus)
app.use(store)
app.mount('#app')
三、使用
methods: {
logOutBtn() {
console.log('退出')
},
increment() {
this.$store.commit('increment')
console.log('hhhhh',this.$store.state.count)
}
},
mounted() {
this.increment()
}