转载的~vuevue

Vue.js状态管理工具Vuex快速上手

2017-03-10  本文已影响6830人  _ihhu

状态管理: 简单理解就是统一管理和维护各个vue组件的可变化状态(你可以理解成 vue 组件里的某些 data)

Vuex是什么

Vuex是集中的应用程序架构为了Vue.js应用程序状态管理。灵感来自 FluxRedux ,但简化的概念和实现是一个专门为 Vue.js 应用设计的状态管理架构。

为什么要用vuex

随着应用复杂度的增加,组件之间传递数据或组件的状态就会越来越多,如果我们按照最基本的方式来进行通信,代码就会变得十分混乱,特别在多人合作的时候。
此时vuex出现了,他就是帮助我们把公用的状态全抽出来放在vuex的容器中,然后根据一定的规则来进行管理

Vuex的核心

Vuex.Store构造器

Vuex 中 Store 的模板化定义如下:


    state:{
        todoLists:[],
    },
    //Getters函数 接受 state 作为其第一个参数
    getters:{
        todoCount(state){
            return state.todoLists.length;
        }
    },
    //和Getters函数一样 接受 state 作为其第一个参数
    mutations:{
        //新増 TodoList item
        ONADDTODO(state,item){
            state.aTodos.push(item);
        },
        //删除 TodoList item
        ONDELTODO(state,index){
            state.aTodos.splice(index,1);
        },
        //设置 错误提示信息
        ONERROR(state,str){
            state.massage=str;
        }
    },
    //Action 函数接受一个与 store 实例具有相同方法和属性的 context 对象,
      因此你可以调用 context.commit 提交一个 mutation,
      或者通过 context.state 和 context.getters 来获取 state 和 getters
    actions:{
        //提交 ONADDTODO
        onAddTodo(context,item){
            if(item.value!=""){
                context.commit("ONADDTODO",item);
                context.commit("ONERROR","");
            }else{
                context.commit("ONERROR","添加失败")
            }
        },
        //提交 ONDELTODO
        //可利用 ES2015 的 [参数解构] 来简化代码
        onDelTodo({commit},index){
            //commit=context.commit
            commit("ONDELTODO",index);
        }

    }

Vuex辅助函数

Vuex辅助函数具体用法参看:API文档

TodoList示例

在理解了 Vuex 的基础概念之后,我们会创建一个todolist来熟悉整个使用流程。

vue init webpack-simple vue-vuex-todolist
npm install 
npm install vuex --save
npm run dev   //启动项目
import Vue from "vue";
import Vuex from "vuex";
Vue.use(Vuex);

const store = new Vuex.Store({
    state:{
        massage:"",
        aTodos:[{value:"默认默认",id:0}],
    },
    getters:{
        nCounts(state){
            return state.aTodos.length;
        }
    },
    mutations:{
        //新増 TodoList item
        ONADDTODO(state,item){
            state.aTodos.push(item);
        },
        //删除 TodoList item
        ONDELTODO(state,index){
            state.aTodos.splice(index,1);
        },
        //设置 错误提示信息
        ONERROR(state,str){
            state.massage=str;
        }
    },
    actions:{
        //提交 ONADDTODO
        onAddTodo(context,item){
            if(item.value!=""){
                context.commit("ONADDTODO",item);
                context.commit("ONERROR","");
            }else{
                context.commit("ONERROR","添加失败")
            }
        },
        //提交 ONDELTODO
        onDelTodo({commit},index){
            commit("ONDELTODO",index);
        }

    },
    modules:{}
});

export default store;
import Vue from 'vue';
import App from './App.vue';
import store from './store';

new Vue({
  el: '#app',
  store,
  render: h => h(App)
})

input.vue

<template>
    <div class="form-group ">
      <input type="text" class="form-control"  placeholder="" v-model="value"  />
      <button type="buttom" class="btn btn-default" @click="addItem">Add Item</button>
    </div>
</template>
<script>
    export default{
        data(){
            return {
                value:"",
                id:0
            }
        },
        methods:{
            addItem(){
                let item={value:this.value,id:++this.id};
                this.value="";
                this.$store.dispatch("onAddTodo",item);
            }
        }

    }
</script>
<style lang="scss">
    %box{display:-webkit-box;-webkit-box-pack:center;-webkit-box-align:center;}
    .form-group{@extend %box;
        .form-control{-webkit-box-flex:1;}
    }
</style>

list.vue

<template>
    <div class="todolist">
      <ul class="list-group">
        <li class="list-group-item" v-for="(item,index) in aTodos" >
            <span>{{item.value}}</span>
            <button class="btn btn-default" @click="delItem(index)">删除</button>
        </li>
       </ul>
    </div>
</template>
<script>
    import {mapState} from "vuex";
    export default{
        data(){
            return {}
        },
        methods:{
            delItem(index){
                this.$store.dispatch('onDelTodo',index);
            }
        },
        computed:{
            ...mapState([
                'aTodos'
            ])
        }
    }
</script>
<style lang="scss">
    %box{display:-webkit-box;-webkit-box-pack:center;-webkit-box-align:center;}
    .list-group-item{display: -webkit-box;-webkit-box-align:center;
        span{
            display: block;
            -webkit-box-flex:1;
        }
    }
</style>
<template>
  <div id="app">
    <h1>Vue.js Vue TodoList</h1>
    <hr>
    <todoInput />
    <todoList />
    <p>todoList 数量:{{todoCount}}</p>
    <pre>{{$store.state}}</pre>
  </div>
</template>

<script>
import todoInput from './components/input.vue';
import todoList from './components/list.vue';
import {mapGetters} from "vuex";

export default {
  name: 'app',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  },
  computed:{
    ...mapGetters({
      todoCount:"nCounts"
    })
  },
  components:{
    todoInput,
    todoList
  }
}
</script>

<style lang="scss">
  %box{display:-webkit-box;-webkit-box-pack:center;-webkit-box-align:center;}
  #app{width:400px;margin:0 auto;}
  h1{text-align:center;}
</style>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>vue-vuex-demo</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  </head>
  <body>
    <div id="app"></div>
    <script src="/dist/build.js"></script>
  </body>
</html>

最后运行查看效果


最后再附上:vuex入门demo无需依赖任何打包工具http://runjs.cn/code/7enrwlef

上一篇 下一篇

猜你喜欢

热点阅读