Vuex中mapState的用法


今天使用Vuex的時候遇到一個坑,也可以說是自己的無知吧,折騰了好久,終於發現自己代碼的錯誤了。真是天雷滾滾~~~~~~

index.js

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都是一樣的道理。牢記~~~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM