4

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
340 声望253 粉丝

前端高级讲师,附上本人随堂视频:[链接]