vuex

Module

store
  index.js
  module
    zj.js

zj.js
  const zj={
    state={},
    getters={},
    mutations={},
    actions={},
  }
  export default zj

index.js
  const store=new Vuex.Store({
    modules:{
      a:zj
    }
  })

mapState(computed) mapGetters(computed) mapMutations(methods) mapActions(methods)輔助函數(shù)

// 
computed:{
  msg(){
    return this.$store.state.msg
  }
}
// 組件的computed無法使用
import mapState from 'vuex'
// 一
computed: mpaState({
  msg(state){
    return state.msg
  }
})
// 二
computed: mapState({
  msg: state=>state.msg,//this.改變 
  msg:'msg' //添加別名

})
// 三
computed:{
  ...mapState(['msg','msg2']),
  // 其它計算屬性
}

state

/* 
  state--存儲組件中的數(shù)據(jù)
  getters--從store中派生出新的數(shù)據(jù)(響應式)
  mutations--同步改變state中的數(shù)據(jù)
  actions--異步獲取數(shù)據(jù)、并提交mutation,通過mutation修改state

  getters:{
    getter名字(state,getters){
      //state store狀態(tài)  getters 其他getter
    }
  }
  mutations:{
    mutation名字(state,payload){
      //通過payload改變state 可以直接修改
    }
  }
  actions:{
    action名字({context},payload){
      //異步獲取數(shù)據(jù),調用context.commit將數(shù)據(jù)提交mutation,mutation接收后修改state
    }
  }


  組件事件中commit mutation
  action中 commit mutation

  組件事件中dispatch action
  組件生命周期鉤子函數(shù) dispatch action

 */


// <div @click='changeState('123')'>
//   {{msg}}
//   {{$store.state.msg}}
// </div>



//store  filter 篩選數(shù)據(jù)
const store=new Vuex.Store({
  state:{
    msg:'數(shù)據(jù)'
  },
  getters:{
    //getters和computed類似  可接收State getters
    msgData(state,getters){
      return getters.msg.filter(item=>item.data)
    },
    getMsgDataById(state){
      //返回一個函數(shù)
      return (id)=>{

      }
    }
  },
  mutations:{
    //提交 同步
    //需要commit 觸發(fā)    對應組件的methods函數(shù)中觸發(fā)mutations this.$store.commit('mutation',數(shù)據(jù))
    //第一個參數(shù)  state   mutation:更改state的唯一
    //第二個參數(shù)  payload  載荷 第二個參數(shù) 傳入的數(shù)據(jù)  盡量為對象
    changeState(state,payload){
      //改變數(shù)據(jù)
      state.msg=payload
    }
  },
  actions:{
    // 分發(fā)  
    // dispatch 
    // 所有異步操作 請求數(shù)據(jù) 通過mutations修改state  不能直接修改數(shù)據(jù)
    syncChange(context,payload){   //context 閹割版store 包含commit、dispatch、getters、state  也可以攜帶載荷
      // 通過mutations修改數(shù)據(jù)
      context.commit('changeState')
      //actions  最后  一定是 commit mutations
    },
    syncChange({context,state},payloa){  //解構賦值 參數(shù)解構  只能夠獲取  不能修改
      commit('changeState')
    }

  }
})

const app=new Vue({
  el:'#app',
  store,
  computed:{
    msg(){
      return this.$store.state.msg
    },
    //函數(shù)名可與store中不一致 
    msgData(){
      return this.$store.getters.msgData
    },
    getMsgDataById(){
      //調用getters返回的函數(shù)
      return this.$store.getters.getMsgDataById(2)
    }
  },
  methods:{
    changeState(payload){
      //出發(fā)mutations中的事件
      this.$store.commit('changeState',payload)
      //對象方式提交
      this.$store.commit({
        type:'changeState',
        數(shù)據(jù):值,
        數(shù)據(jù)2:值 
      })
    },
    syncChange(){
      // 分發(fā)
      this.$store.dispatch('syncChange')
    }
  }
})

Mutation 更改vuex store中狀態(tài)的唯一方法

字符串mutation名 常量

const str='abc'
const obj={
  ['字符串']:(){}, //可以用字符串表示方法名
  [str]:(){}
}

const mutationsName={
  setName:'setName',
  setAge:'setAge'
}
const store=new Vuex.Store({
  store,
  mutations:{
    [mutationsName.setName](){

    }
  }
})
const app=new Vue({
  el:'#app',
  methods:{
    fn(){
      this.$store.commit(mutationsName.setName)
    }
  }
})

//mutation-type.js  存儲所有mutations名 直接 修改 設置state 
export const SOME_MUTATION='SOME_MUTATION'

Actions

cli中使用vuex

/* 
//組件化store
store
  index.js
  modules
    各組件對應的store

// cli中使用vuex
src文件夾下新建store文件夾及index.js
  import Vue from 'vue'
  import Vuex from 'vuex'

  Vue.user(Vuex)

  const state={}
  const getters={}
  const mutations={}
  const actions={}

  const store = new Vuex.Store({
    state,getters,mutations,actions
  })

  export default store
main.js
  import store from ' xxx '

  const app=new Vue({
    store
  })

*/
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • ### store 1. Vue 組件中獲得 Vuex 狀態(tài) ```js //方式一 全局引入單例類 // 創(chuàng)建一...
    蕓豆_6a86閱讀 794評論 0 3
  • 安裝 npm npm install vuex --save 在一個模塊化的打包系統(tǒng)中,您必須顯式地通過Vue.u...
    蕭玄辭閱讀 3,054評論 0 7
  • 目錄 - 1.什么是vuex? - 2.為什么要使用Vuex? - 3.vuex的核心概念?||如何在組件中去使用...
    我跟你蔣閱讀 4,246評論 4 51
  • Vuex 的學習記錄 資料參考網(wǎng)址Vuex中文官網(wǎng)Vuex項目結構示例 -- 購物車Vuex 通俗版教程Nuxt....
    流云012閱讀 1,555評論 0 7
  • vuex是一個狀態(tài)管理模式,通過用戶的actions觸發(fā)事件,然后通過mutations去更改數(shù)據(jù)(你也可以說狀態(tài)...
    Ming_Hu閱讀 2,141評論 3 3

友情鏈接更多精彩內容