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無(wú)法使用
import mapState from 'vuex'
// 一
computed: mpaState({
  msg(state){
    return state.msg
  }
})
// 二
computed: mapState({
  msg: state=>state.msg,//this.改變 
  msg:'msg' //添加別名

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

state

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

  getters:{
    getter名字(state,getters){
      //state store狀態(tài)  getters 其他getter
    }
  }
  mutations:{
    mutation名字(state,payload){
      //通過(guò)payload改變state 可以直接修改
    }
  }
  actions:{
    action名字({context},payload){
      //異步獲取數(shù)據(jù),調(diào)用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){
      //返回一個(gè)函數(shù)
      return (id)=>{

      }
    }
  },
  mutations:{
    //提交 同步
    //需要commit 觸發(fā)    對(duì)應(yīng)組件的methods函數(shù)中觸發(fā)mutations this.$store.commit('mutation',數(shù)據(jù))
    //第一個(gè)參數(shù)  state   mutation:更改state的唯一
    //第二個(gè)參數(shù)  payload  載荷 第二個(gè)參數(shù) 傳入的數(shù)據(jù)  盡量為對(duì)象
    changeState(state,payload){
      //改變數(shù)據(jù)
      state.msg=payload
    }
  },
  actions:{
    // 分發(fā)  
    // dispatch 
    // 所有異步操作 請(qǐng)求數(shù)據(jù) 通過(guò)mutations修改state  不能直接修改數(shù)據(jù)
    syncChange(context,payload){   //context 閹割版store 包含commit、dispatch、getters、state  也可以攜帶載荷
      // 通過(guò)mutations修改數(shù)據(jù)
      context.commit('changeState')
      //actions  最后  一定是 commit mutations
    },
    syncChange({context,state},payloa){  //解構(gòu)賦值 參數(shù)解構(gòu)  只能夠獲取  不能修改
      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(){
      //調(diào)用getters返回的函數(shù)
      return this.$store.getters.getMsgDataById(2)
    }
  },
  methods:{
    changeState(payload){
      //出發(fā)mutations中的事件
      this.$store.commit('changeState',payload)
      //對(duì)象方式提交
      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  存儲(chǔ)所有mutations名 直接 修改 設(shè)置state 
export const SOME_MUTATION='SOME_MUTATION'

Actions

cli中使用vuex

/* 
//組件化store
store
  index.js
  modules
    各組件對(duì)應(yīng)的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
  })

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

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容