vue mapStateの使い方

1299 ワード

import Vue from 'vue'
import Vuex from 'vuex'
import mutations from './mutations'
import actions from './action'
import getters from './getters'


Vue.use(Vuex)


const state = {
    userInfo: { phone: 111 }, //    
    orderList: [{ orderno: '1111' }], //    
    orderDetail: null, //      
    login: false, //    
}


export default new Vuex.Store({
    state,
    getters,
    actions,
    mutations,
})
 
   
   
  
computed: {
    ...mapState([
        'orderList',
        'login'
     ]),
 },   
 mounted(){  
     console.log(typeof orderList); ==>undefind
     console.log(typeof this.orderList)==>object
 }

mapStateは拡張演算子によってstore.state.orderListマッピングthis.orderListというthisは重要で、このマッピングは現在のVueのthisオブジェクトに直接マッピングされます.だからthisを通じてこれらのオブジェクトをポイントすることができて、同じ理屈で、mapActions、mapMutationsはすべて同じ道理です.