vue 状态管理 二、状态管理的基本使用
系列导航
状态管理的基本使用
一、 效果
定义一个变量counter,多个组件都使用它,在一个地方改变了个counter的值,其他组件中的值都跟着变更。
二、 目录结构
三、源码
index.js
import {createStore} from 'vuex'
export default createStore({
state: {
counter: 0,
},
mutations: {
increament(state) {
state.counter++
},
decrement(state) {
state.counter--
}
},
actions: {},
modules: {}
})
App.vue
-------app.vue 中的数据----------
{{$store.state.counter}}
-------hello-world 组件中的数据----------
HelloWorld.vue
{{$store.state.counter}}