Vuex

作者: 廖小芝 | 来源:发表于2020-03-12 14:43 被阅读0次

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>

相关文章

  • VUEX基本介绍,包含实战示例及代码(基于Vue2.X)

    VUEX 1 VUEX基本介绍 1.1 官方API 1.2 什么是vuex 1.3 Vuex使用场景 1、Vuex...

  • 【文档笔记】-Vuex

    什么是vuex? vuex文档: https://vuex.vuejs.org/zh/[https://vuex....

  • vuex配置

    vuex配置 目录 vuex的五个核心 配置vuex vuex持久化 一、vuex五个核心概念 State 定义状...

  • Vuex

    安装Vuex cnpm install vuex --save-dev Vuex是什么? 这是[Vuex的官网](...

  • Vuex

    1.Vuex概述 2.Vuex基本使用 3.使用Vuex完成todo案例 1.Vuex概述 Vuex是实现组件全局...

  • vuex

    Vuex介绍: Vuex官网:http://vuex.vuejs.org/ Vuex是实现数据状态管理的技...

  • vuex+axios 的开发流程记录

    相关文档 vuex: https://vuex.vuejs.org/zh/ 是否有必要使用vuex vuex是vu...

  • 2019-06-07

    import Vue from 'vue'import Vuex from 'vuex'Vue.use(Vuex)...

  • 配置 vuex 和 vuex 本地持久化

    配置 vuex 和 vuex 本地持久化 目录 vuex是什么 vuex 的五个核心概念State 定义状态(变量...

  • vuex

    配置 vuex 和 vuex 本地持久化 目录 vuex是什么 vuex 的五个核心概念State 定义状态(变量...

网友评论

      本文标题:Vuex

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