Vuex是Vue的状态管理工具,通过Vuex可以让Vue页面中所有的页面获取到公共的数据。
核心属性及使用方法
1、state:存放数据的地方,类似于vue中的data
(1)直接获取
<p>获取count --{ { $store.state.count }}</p>
(2)通过mapState获取,此方法需要在computed中调用
//导入mapState
import { mapState } from "vuex";<p>mapState获取count--{ { count }}</p>
computed:{
...mapState(["count"])
}
2、getters:从基本数据(state)派生的数据,相当于state的计算属性,此方法调用同样有两种方法
(1)直接获取
<p>直接使用--{ { $store.getters.count }}</p>
(2)通过mapGetters获取,同样在computed中调用
<p>mapGetters获取--{ { count }}</p>
computed:{
...mapGetters(["count"]),
}