vuex中state的使用
vuex中定义state
state: {
count:2,
hername: 'JOHH'
}
组件中读取vuex中的数据
方式1:this.$store.state.全局数据名称
<p>{{this.$store.state.count}}</p>
方式2:从vuex中按需导入mapState
函数
通过刚才导入的mapState
函数,将当前组件需要的全局数据,映射为当前组件的computed
计算属性
<p>使用mapAtate:{{count}}</p>
<p>this is {{hername}}</p>
import {mapState} from 'vuex'
computed: {
...mapState(['count','hername'])
}