美文网首页
vuex详解

vuex详解

作者: 我爱阿桑 | 来源:发表于2019-08-05 19:11 被阅读0次
image.png
  • 在vue中我们通过vuex来管理状态,该值一但被修改,所有引用该值的地方都会自动更新
    项目的src文件下的store文件中新建一个index.js文件如下:
import  Vue  from ' vue'
import  Vuex from 'vuex'
//使用vuex
vue.use(Vuex)vuex
//创建vuex实例
const  store=new vuex.Store({

})
export default store
  • 然后我们在main.js文件中引入该文件,在文件里面添加 import store from ‘./store’;,再在vue实例全局引入store对象;
import Vue from 'vue'
import store from '/store'
new vue({
store
})

State

  • vuex的数据源,在页面获取需要使用this.$store.state来获取state数据
import  Vue  from ' vue'
import  Vuex from 'vuex'
//使用vuex
vue.use(Vuex)vuex
//创建vuex实例
const  store=new vuex.Store({
    state:{
          count:1
    }
})
export default store
  • 在页面就可以使用this.$store.state.count来获取数据
    还可以这样写
<script>
import { mapState, mapMutations} from 'vuex'

export default{
computed:{
     // ...mapState(['count'])   
    // ...mapState({
     //     total:'total'
   //  })
     // 推荐使用这种
      ...mapState({
          count:(state)=>state.count
    })
 }
}
<script>

Getters

  • Getter相当于vue中的computed计算属性,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算,这里我们可以通过定义vuex的Getter来获取,Getters 可以用于监听、state中的值的变化,返回计算后的结果.
import  Vue  from ' vue'
import  Vuex from 'vuex'
//使用vuex
vue.use(Vuex)vuex
//创建vuex实例
const  store=new vuex.Store({
    state:{
          count:1
    },
getters:{
     getStateCount:function(state){
         return state.count+1
      }
    }
})
export default store
  • 在页面获取使用this.$store.getters.getStateCount
    使用辅助函数后 在行间就可以使用这个key值 getStateCount,来读取了
   import { mapGetters} from 'vuex'
   export default{
     computed:{
          // 取值一定要在computed中,getter和state都要在计算属性中
         //    ...mapGetters(['getStateCount'])
          ..mapGetters({
                 getStateCount:'getStateCount'
            })
      }

}

Mutations

  • 如果想更改state的值如何修改,使用mutations
import  Vue  from ' vue'
import  Vuex from 'vuex'
//使用vuex
vue.use(Vuex)vuex
//创建vuex实例
const  store=new vuex.Store({
    state:{
          count:1
    },
  getters:{
       getStateCount:function(state){
           return state.count+1
         }
     },
    mutations: {
         AddCount(state) {
              return (state.count +=1)
       },
        ReduceCount(state) {
              return (state.count -= 1)
       }
}
})
export default {
 store,
 mutations,
 getters

}
  • 那在界面如何调用mutations呢?
<template>
  <div class="hello">
    <h3>{{$store.state.count}}</h3>
    <div>
      <button @click="AddClick(10)">增加</button>
      <button @click="ReduceClick(10)">减少</button>
    </div>
  </div>
</template>
<script>
import { mapState, mapMutations} from 'vuex'

export default{
methods: {
...mapMutations({
       'AddCount':'AddCount',
       'ReduceCount':'ReduceCount'
    })
    handleAddClick(n){
         // 第一种方式
         // this.$store.commit('AddCount');
          // 第二种方式
         this.AddCount()
    },
    handleReduceClick(n){
      //  this.$store.commit('ReduceCount');
      this.ReduceCount()
    }
  }
}
<script>

Actions

  • 通过mutations达到了修改store中状态值的目的,但是,官方并不推荐我们这样直接去修改store里面的值,而是让我们去提交一个actions,在actions中提交mutation再去修改状态值
import  Vue  from ' vue'
import  Vuex from 'vuex'
//使用vuex
vue.use(Vuex)vuex
//创建vuex实例
const  store=new vuex.Store({
    state:{
          count:1
    },
  getters:{
       getStateCount:function(state){
           return state.count+1
         }
     },
    mutations: {
         AddCount(state,n) {
              return (state.count +=n)
       },
        ReduceCount(state,n) {
              return (state.count -= n)
       },
   actions :{
       AddCount100(context, n ) {
             context.commit('AddCount', n)
    },
     ReduceCount100(context, n {
            context.commit('ReduceCount', n)
    }
}
}
})
export default {
 getters,
  state,
  mutations,
  actions
}
  • 所以点击触发方法可以改为:
<template>
  <div class="hello">
    <h3>{{$store.state.count}}</h3>
    <div>
      <button @click="AddClick(10)">增加</button>
      <button @click="ReduceClick(10)">减少</button>
    </div>
  </div>
</template>
<script>
import { mapState, mapMutations ,mapActions} from 'vuex'

export default{
methods: {
// ...mapActions(['AddCount100','ReduceCount100'])
...mapActions({
       'AddCount100':'AddCount100',
       'ReduceCount100':'ReduceCount100'
})
...mapActions({
   
})
    handleAddClick(n){
     var  n=10;
     //  this.$store.dispatch('AddCount100',n)
     this.AddCount100(n)
    },
    handleReduceClick(n){
       var  n=10;
       // this.$store.dispatch('ReduceCount100',n)
       this.ReduceCount100(n)
    }
  }
}
<script>


相关文章

  • Vuex-Vue状态管理模式

    参考:Vuex详解。 1. Vuex及其作用 Vuex:是一个专为 Vue.js应用程序开发的状态管理模式。它采用...

  • vuex使用记录

    副标题:vuex使用详解、vue使用全局变量、vue使用 store 这篇博客主要记录了 vuex的使用方法,简单...

  • Vuex详解

    Vuex是什么 状态管理模式,将所有组件需要共享的变量全部存储在一个对象里,然后将这个对象放在顶层组件中供其他组件...

  • vuex详解

    在vue中我们通过vuex来管理状态,该值一但被修改,所有引用该值的地方都会自动更新项目的src文件下的store...

  • VUEX 详解

    为什么使用vuex 在中大型应用中,应用的各个组件间需要进行数据传递,使用传统方式繁琐且不可控 Vuex 为所有组...

  • vuex详解

    一、什么是vuex ? 答: 通俗的来说,我们可以把全局需要用到的数据【例如: 数据 a】放在vuex中,同理,v...

  • 详解VUEX

    安装vuex 安装完之后新建一个store.js文件

  • vuex 详解

    1、vuex 是什么? 集中式存储管理应用的所有组件的状态(单独出来管理共用的状态,如:token的管理等,相当于...

  • VUE知识点总结

    一、生命周期详解(八个钩子函数) 二、vuex状态管理 Vuex 是一个专为 Vue.js 应用程序开发的状态管理...

  • vuex的详解

    技术胖的vuex详解,说的很详细易懂,收藏了,觉得还是记录一下 http://jspang.com/2017/05...

网友评论

      本文标题:vuex详解

      本文链接:https://www.haomeiwen.com/subject/uyfvdctx.html