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

DK_Lan發表於2018-04-18

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>

相關文章