vuex-store-5.0

                                         vuex-store解析

5.0------vuex共享 是插件 Single Source of Truth单一状态树--》单一数据源 即只推荐使用一个store

  1.  store文件下 index.js

    import Vue from 'vue'

    import Vuex from 'vuex'


    1.1.安装插件

      vue.use(Vuex)

    1.2.创建对象

    const store = new Vuex.Store({     

      state:{                                                                  Action ->Mutations(先进行修改) ->State(不要在这进行修改)

          counter:1000                                                Devtools插件 监控state的改变(只能监控同步操作,直接修改state是监控不了)

          student:[

            {name:'1',age:18},                                       

            {name:'3',age:22},

            {name:'2',age:30}

          ],

          info:{

              name:'cobe',

              age:33,

              hobby:hehe

          }

      },-----------------------------------------------

        mutations:{                         

            同步操作 

            // 定义方法  只要对state进行修改的内容 都在这里进行状态更新

          increment(state){                          在App.vue中用  this.$store.commit('increment') 获取

            state.counter++

          },

          decrement(state){                          在App.vue中用 this.$store.commit('decrement') 获取

            state.counter--

          },

          incrementCount(state,count){            在App.vue中  <button @click="addCount(5)">+5</button>

            state.counter += count                    addCount(count){  1.普通的提交封装

          }                                                                this.$store.commit(' incrementCount',count)

          ,                                                                }

            incrementCount2(state,payload){            在App.vue中  <button @click="addCount(5)">+5</button>

            state.counter += payload.count            addCount(count){    1.特殊的提交封装

          }                                                                        this.$store.commit({

                                                                                        type:'incrementCount2', count

                                                                                    })

          ,                                                                    }

          updataInfo(state){

                state.info.name = 'coderwh'              响应式

              // state.info['address'] = '洛杉矶'            增加一个属性  没有响应式

              //  Vue.set(state.info,'address','洛杉矶')    增加一个属性  具有响应式

              //  delete state.info.age                          删除一个属性  没有响应式

              //  Vue.delete(state.info,'age')                删除一个属性  具有响应式

        }, 

          addStudent(state,stu){                          在App.vue中  <button @click="addstudent">添加学生</button>

          state.student.push(stu)                        addstudent(){  const stu ={name:'4'.age:20}

          }                                                                                  this.$store.commit('addStudent',stu)

        },                                                        },-----------------------------------------------

        actions:{  异步操作      类似于mutations  修改state内容时,要经过mutations  在App.vue中  this.$store.dispatch('aupdataInfo',参数)

            aUpdateInfo(context,payload(参数)){    context:上下文 相当于store           

              setTimeout(()=>{

                  context.commit('updataInfo')

              },1000)

            },

            异步请求后 ,通知外界

      2.1    aupdateInfo(context,payload){    context:上下文 相当于store           

              setTimeout(()=>{                                                  在App.vue中  this.$store.dispatch('aupdataInfo',{

                  context.commit('updataInfo')  里面提交完成                                      message:'携带的信息'

                    console.log(payload.message)                                                        success:()=>{      console.log('里面已完成')

                    payload.success()  回调                                                                    }

              },1000)                                                                                          })   

            }, 

    2.2    aupdateInfo(context,payload){    context:上下文 相当于store

                return new Promise((reslove,reject)={                    在App.vue中  this.$store.dispatch('aupdataInfo',‘携带的信息')

                  setTimeout(()=>{                                                                          .then(res=>{

                    context.commit('updataInfo')    里面提交完成                              console.log('里面完成了提交')

                    console.log(payload)                                                                  console.log(res)      打印  hahah

                    reslove('hahah')  向外界传入参数                                                })             

              },1000)     

            })                                                                                       

          },                                                                                                           

        },,-----------------------------------------------                                                                                                             

        getters:{

        类似于计算属性 进行操作但不对state进行修改  eg:filter 过滤器

        powerCounter(state){                      在App.vue中      <h2>{{$store.getters.powerCounter}}<h2>

          return state.counter * state.counter

        },

        more20stu2(state){                          在App.vue中  <h2>{{$store.getters.more20stu2}}<h2>

          return state.students.filter(s=>s.age >20) 

        },

        more20stu2Length(state,getters){    在App.vue中  <h2>{{$store.getters.more20stu2Length}}<h2>

          return getters.more20stu2.length

      },

      moreAgeStu(state){                            在App.vue中  <h2>{{$store.getters.moreAgeStu(18)}}<h2>  传入一个参数

        return function(age){

          return state.student.filter(s=>s.age>age)

        }

      }

      },-----------------------------------------------

        module:{                                      const moduleA={                                    在App.vue中  <h2>{{$store.state.a.name}}<h2>                     

            划分专门模块 进行保存                  state:{  name:'zhangsan'},

          a:moduleA                                    mutations:{                                        在App.vue中    <button @click="updateName">修改名字</button>

        }                                                            updateName(state,payLoad){                  updateName(){  this.$store.commit(' updateName','lisi') }

                                                                        state.name = payLoad

                                                                      }

                                                                    },

                                                                actions:{

                                                                    aUpdateName(context){            这里的    context是本模块   

                                                                    }                                              如果想调用根模块就调用  context.rootGetters

                                                                },

                                                                getters:{                                             

                                                                      fullname(state){                            在App.vue中  <h2>{{$store.getters.fullname}}<h2>   

                                                                          return state.name + 'hahhah'

                                                                      },

                                                                      fullname2(state,getters){                在App.vue中  <h2>{{$store.getters.fullname2}}<h2>   

                                                                          return getters.fullname + 'hahhah2'

                                                                      },

                                                                      引用大模块中state的数据-----

                                                                      fullname3(state,getters,rootstate){    在App.vue中  <h2>{{$store.getters.fullname3}}<h2>   

                                                                          return getters.fullname + rootstate.counter

                                                                      }

                                                                  }

    })                                                             

    default export store  -----------------------------------------------

  2.在main.js中  添加

    import store from './store'

    new Vue({

      el:'#app',

      store,            添加这行

      render:h =>h(App)

    })

  3.在App.vue 中          -----------------------------------------------

  3.1  <template>

        <h2>{{$store.state.counter}}<h2>

        <button @click="$store.state.counter++">+</button>

        <button @click="$store.state.counter++">-</button>

    </template>

    3.2  <template>

        <h2>{{$store.state.counter++}}<h2>

        <button @click="additon">+</button>

        <button @click="subtraction">-</button>

        <h2>{{$store.getters.powerCounter}}<h2>

        <h2>{{more20stu}}<h2>

    </template>

    <script>

      export default{

        computed:{

          more20stu(){

            return this.$store.state.students.filter(s=>{

              return s.age >= 20

          })

        }

      },

        methods:{

          additon(){

            this.$store.commit('increment')

          },

          subtraction(){

            this.$store.commit('decrement')

          },

        }

      }

    </script>

5.x-----------es6语法  对象结构

  const obj = {

      name:'why',

      age:18,

      height:1.9

    }

  const {name,height,age} =obj;  顺序可以调换

  console.log(obj.name) =>why    console.log(obj.age) =>18  console.log(obj.height) =>1.9

例子:局部状态通过context.state 暴露出来,根节点状态则为context.rootState    在上述module中有描述

const moduleA = {

  actions:{

    hahaha({state,commit,rootState})

        console.log(rootState)    打印出来的是根节点对应的内容

  },

  getters:{

    hahaha({state,commit,rootState})

      return state.count + rootState.count    即本状态下的count 加上 根节点上 count

        console.log(rootState)    打印出来的是根节点对应的内容

  }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 复盘做一些微调整,发现大部分人看盘以同花顺为主,板块分析方面从今天开始跟踪同花顺热点板块,以前都是做的通达信板块复...
    三少爷的自修之路阅读 263评论 0 2
  • 在生活中,当某个人的某些特点非常突出或者与家中长辈比较相似时,大家都会说:这是天生的,这是遗传!好像每个人都对遗传...
    康有趣阅读 199评论 0 0
  • 习惯了老婆在家的日子,她总是在我生活的每个角落存在,在视觉范围内的时候,我能看见她,不在眼前时能感受她的气息,...
    苍景流年_5cbf阅读 665评论 4 6
  • 大雁远飞万里,只为寻觅一个温暖的家?那么,人历尽苦难彷徨,为的又是什么呢? 风荷惨淡,梧桐摇落,浅草悠悠满路。...
    留觅阅读 278评论 0 2