Vue 教程第十三篇—— Vuex 之 state

Vuex——state访问

方法一、直接获取

<span>{{$store.state.count}}</span>

方法二、通过 computed 的计算属性直接赋值

<span>{{count}}</span>
<script type="text/javascript">
    computed: {
        count(){
            return this.$store.state.count
        }
    } 
</script>

方法三、通过 mapState 的计算属性直接赋值

<span>{{count}}</span>
<script type="text/javascript">
    import {mapState} from 'vuex';
    computed: mapState({
        count: state => state.count
    })
</script>

方法四、通过 mapState 的数组来赋值

<span>{{count}}</span>
<script type="text/javascript">
    import {mapState} from 'vuex';
    computed: mapState(['count'])
</script>

方法五、对象展开运算符

<span>{{count}}</span>
<script type="text/javascript">
    import {mapState} from 'vuex';
    computed: {
        ...mapState({
            count: state => state.count
        })
    }
</script>
    原文作者:DK_Lan
    原文地址: https://segmentfault.com/a/1190000014463294
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞