Vuex

Vuex

1、State单一状态数
//store/index.js
const store = new Vuex.Store({
    state:{
        counter:200
    },
    
})
//app.vue
<h2>{{$store.state.counter}}</h2>
//200
2、getters基本使用

获取state变异后的状态

//store/index.js
const store = new Vuex.Store({
    state:{
        counter:200
    },
    getters:{
        qCounter(state){
            return state.counter * state.counter
        }
    }
})
//app.vue
<h2>{{$store.getters.qCounter}}</h2>
//40000
  • getters传参
const store = new Vuex.Store({
    state:{
        counter:200,
        students:[
            {id:110,name:"xxx1",age:10},
            {id:111,name:"xxx2",age:15},
            {id:112,name:"xxx3",age:20},
            {id:113,name:"xxx4",age:25}
        ]
    },
    getters:{
        qCounter(state){
            return state.counter * state.counter
        },
        more20stu(state){
            return state.students.filter(s => s.age >=20 )
        },
        more20stuLenght(state,getters){
            return getters.more20stu.length
        },
        moreAgeStu(state){
            return function(age){
                return state.students.filter(s => s.age >= age)
            }
        }
    }
})

//app.vue
    <h2>{{$store.getters.qCounter}}</h2>
    <p>{{$store.getters.more20stu}}</p>
    <p>{{$store.getters.more20stuLenght}}</p>
    <p>{{$store.getters.moreAgeStu(15)}}</p>
image.png
3、mutations基本使用

Vuex的store状态的更新唯一方式:提交mutation

//store/index.js
const store = new Vuex.Store({
    state:{
        counter:200
    },
    mutations:{
        // 方法
        increment(state){
            state.counter++
        },
        decrement(state){
            state.counter--
        }
    }
})

//app.vue
    <button @click="addclick">+</button>
    <button @click="subclick">-</button>

export default {
  name: 'App',
  components:{
    HelloTest
  },
  data(){
    return {
    }
  },
  methods:{
    addclick(){
      this.$store.commit('increment')
    },
    subclick(){
      this.$store.commit('decrement')
    }
  }
}

  • mutations 传参
//store/index.js
const store = new Vuex.Store({
    mutations:{
        // 方法
        incrementCount(state,count){
            state.counter += count
        }
    }
})

//app.vue
export default {
  methods:{
    addcount(count){
      this.$store.commit('incrementCount',count)
    }
  }
}
<h2>{{$store.state.counter}}</h2>
<button @click="addcount(5)">+5</button>

mutaition 不可进行异步操作

4、Action的基本定义

用来替代mutation进行异步操作

//app.vue
<h2>{{$store.state.info}}</h2>
<button @click="updataInfo">修改信息</button>

export default {
    methods:{
    updataName(){
      this.$store.commit('updataName',"lili")
    }
  }
}
// index.js
const store = new Vuex.Store({
  mutations:{
        // 方法
        updateInfo(state){
            state.info.name ="lxz0026"
        }
    },
    actions:{
        aUpdataInfo(context,payload){
            return new Promise((resolve ,reject) => {
                setTimeout(()=>{
                    context.commit('updateInfo');
                    console.log(payload);
                    resolve("111")
                },1000)
            })
            
        }
    }
})
5、Modules的基本定义

模块化

//moduleA.js

export default {
    state:{
        name:"moduleA"
    },
    mutations:{
        updataName(state,payload){
            state.name = payload;
        }
    }
}

//index.js
import moduleA from './modules/moduleA'
const store = new Vuex.Store({
    state,
    mutations,
    actions,
    getters,
    modules:{
        a:moduleA
    }
})

//app.vue
<h2>{{$store.state.a.name}}</h2>
<button @click="updataName">module修改名字</button>

<script>
export default {
  methods:{
    updataName(){
      this.$store.commit('updataName',"lili")
    }
  }
}
</script>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • ### store 1. Vue 组件中获得 Vuex 状态 ```js //方式一 全局引入单例类 // 创建一...
    芸豆_6a86阅读 4,055评论 0 3
  • Vuex是什么? Vuex 是一个专为 Vue.js应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件...
    萧玄辞阅读 8,352评论 0 6
  • 安装 npm npm install vuex --save 在一个模块化的打包系统中,您必须显式地通过Vue.u...
    萧玄辞阅读 8,046评论 0 7
  • Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应...
    白水螺丝阅读 10,135评论 7 61
  • Vuex的五个核心概念 本文参考自Vue文档,说的非常详细,建议看文档。 Vuex是什么? VueX 是一个专门为...
    一二三四五_6ce3阅读 5,084评论 0 0

友情链接更多精彩内容