vuex(篇1)——state

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'])
}

猜你喜欢

转载自blog.csdn.net/weixin_40119412/article/details/107560332
今日推荐