今天使用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都是一樣的道理。牢記~~~
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。
新聞熱點
疑難解答
圖片精選