vuex 上手使用

什么是vuex

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。

每一个 Vuex 应用的核心就是 store(仓库)。先看一个简单例子:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  }
})
store.commit('increment')
console.log(store.state.count) // -> 1

核心概念

State

State 定义状态(变量)

在vue 组件中使用

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    count: 0
  }
})

new Vue({
    el:'#app',
    store,
    computed:{
        count(){
            return store.state.count
        }
    }
})

mapState 辅助函数

import {mapState} from "vuex"
new Vue({
    el:'#app',
    store,
    computed:mapState({
        count:state => state.count,
    })
})

当计算属性与 state 属性名称相同时,可以写成:

new Vue({
    el:'#app',
    store,
    computed:mapState(['count'])
})

与局部计算属性混合使用

new Vue({
    el:'#app',
    store,
    computed:{
        local(){},
        ...mapState(['count'])
    }
})

Getter

像计算属性一样,getter 的返回值会根据它的依赖被缓存起来,且只有当它的依赖值发生了改变才会被重新计算。

const store = new Vuex.Store({
  state: {
    todos: [
      { id: 1, text: '...', done: true },
      { id: 2, text: '...', done: false }
    ]
  },
  getters: {
    doneTodos: state => {
      return state.todos.filter(todo => todo.done)
    }
  }
})

new Vue({ 
    el: '#app',
    store,
    computed: {
        doneTodos(){
            return store.getters.doneTodos
        }
    }
});

mapGetters 辅助函数

import {mapGetters} from "vuex"
new Vue({ 
    el: '#app',
    store,
    computed: mapGetters(['doneTodos'])
    // 混合使用
    computed: {
        local(){},
        ...mapGetters(['doneTodos'])
    }
});

Mutation

Vuex 的 store 中的状态的唯一方法是提交 mutation。mutation 必须是同步函数。

const store = new Vuex.Store({
  state: {
    count: 1
  },
  mutations: {
    increment (state,payload) {
      state.count += payload.num
    }
  }
})

store.commit('increment', {
  num: 10
})

在组件中使用

使用 this.$store.commit('xxx') 提交 mutation, 或者使用 mapMutations

import { mapMutations } from 'vuex'

export default {
  // ...
  methods: {
    ...mapMutations([
      'increment', /* 将 `this.increment()` 映射为`this.$store.commit('increment')`*/
      'incrementBy' /* 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)`*/
    ]),
    ...mapMutations({
      add: 'increment' /* 将 `this.add()` 映射为 `this.$store.commit('increment')`*/
    })
  }
}

使用常量替代 Mutation 事件类型

// mutation-types.js
export const INCREMENT = 'INCREMENT'

// store.js
import Vuex from 'vuex'
import { INCREMENT } from './mutation-types'

const store = new Vuex.Store({
  state: { ... },
  mutations: {
    [INCREMENT] (state,payload) {
      state.count += payload.count
    }
  }
})

// app.vue
<template>
  <div id="app">
    <p>{{ count }}</p>
    <button @click="increment({ count: 5 })">+5</button>
  </div>
</template>
<script>
import { INCREMENT } from './mutation-types'
import { mapMutations } from 'vuex'

export default {
  // ...
  methods: {
    ...mapMutations({
        increment:INCREMENT
    })
  }
}
</script>

actions

Action 类似于 mutation,不同在于:

  • Action 提交的是 mutation,而不是直接变更状态。
  • Action 可以包含任意异步操作。
const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  },
  actions: {
    increment (context) {
        setTimeout(()=>{
            context.commit('increment')
        },1000)
    }
  }
})

在组件中使用

Action 通过 store.dispatch 方法触发 或者使用 mapActions

export default {
  // ...
  methods: {
    increment(){
        this.$store.dispatch('increment')
    },
    ...mapActions(['increment']),
    ...mapActions({
        add:'increment'
    })
  }
}

Module

当应用变得非常复杂时,store 对象变得相当臃肿。我们可以使用 module, 每个模块拥有自己的 stategettermutationaction

const moduleA = {
  state: () => ({
    count:0
  }),
  mutations: {
    incrementA(state){
        state.count += 1
    }
  },
  actions: { ... },
  getters: {
    doubleCount(state){
        return state.count * 2
    }
  }
}

const moduleB = {
  state: () => ({
    count:1
  }),
  mutations: {
    incrementB(state){
        state.count += 2
    }
  },
  actions: { ... }
}

const store = new Vuex.Store({
  modules: {
    a: moduleA,
    b: moduleB
  }
})


store.state.a.count // -> 0
store.commit('incrementA')
store.state.a.count // -> 1
store.getters.doubleCount // -> 2
store.state.b.count // -> 1
store.commit('incrementB')
store.state.b.count // -> 3

命名空间

如果把上面例子中 ncrementAincrementB 改成 increment,这就需要引入命名空间。默认情况下,模块内部的 gettermutationaction 是注册在全局命名空间的。

添加namespaced:true就可以使用带命名空间的模块。带命名空间模块访问全局内容,rootState 和 rootGetters 作为第三和第四参数传入 getter,也会通过 context 对象的属性传入 action。

在全局命名空间分发 action 或 提交 mutation , 将 {root:true} 作为第三参数传给 dispatchcommit

const moduleA = {
  namespace:true,
  state: () => ({
    count:1
  }),
  mutations: {
    increment(){ ... } // -> commit('a/increment')
  },
  actions: {
    asyncIncrement({dispatch, commit, getters, rootGetters}){
        getters.doubleCount // -> 'a/doubleCount' -> 2
        rootGetters.doubleCount // -> 'doubleCount'

        dispatch('someOtherAction') // -> 'a/someOtherAction'
        dispatch('someOtherAction', null, { root: true }) // -> 'someOtherAction'

        commit('increment') // -> 'a/increment'
        commit('increment', null, { root: true }) // -> 'increment'
    },
    someOtherAction(ctx,payload){ ... }
  },
  getters:{
    doubleCount(state, getters, rootState, rootGetters){
        getters.someOtherGetter // -> 'a/someOtherGetter'
        rootGetters.someOtherGetter // -> 'someOtherGetter'
        return state.count * 2
    },
    someOtherGetter: state => { ... }
  }
}
const moduleB = {
  namespace:true,
  state: () => ({
    count:0
  }),
  mutations: {
    increment(){ ... } // -> commit('b/increment')
  },
  actions: { ... }
}
const store = new Vuex.Store({
  modules: {
    a: moduleA,
    b: moduleB
  },
  getters:{
    someOtherGetter: state => { ... }
  }
  mutations: {
    increment(){ ... } // -> commit('increment')
  },
  actions:{
    someOtherAction(ctx,payload){ ... }
  }
})
带命名空间的模块注册全局的action

注册全局 action ,需添加 root:true,并将action的定义放在 函数handler中。

{
  actions: {
    someOtherAction ({dispatch}) {
      dispatch('someAction')
    }
  },
  modules: {
    foo: {
      namespaced: true,

      actions: {
        someAction: {
          root: true,
          handler (namespacedContext, payload) { ... } // -> 'someAction'
        }
      }
    }
  }
}
组件中使用带命名空间的模块

在组件中使用带命名空间的模块,mapState, mapGetters, mapActionsmapMutations 写法比较繁琐。解决方法,将模块的空间名称字符串作为第一个参数传入。

computed: {
  ...mapState({
    a: state => state.some.nested.module.a,
    b: state => state.some.nested.module.b
  })
},
methods: {
  ...mapActions([
    'some/nested/module/foo', // -> this['some/nested/module/foo']()
    'some/nested/module/bar' // -> this['some/nested/module/bar']()
  ])
}
// 简化
computed: {
  ...mapState('some.nested.module',{
    a: state => state.a,
    b: state => state.b
  })
},
methods: {
  ...mapActions('some/nested/module',[
    'foo', // -> this.foo()
    'bar' // -> this.bar()
  ])
}

也可以通过createNamespacedHelpers 创建基于某个命名空间辅助函数,返回一个对象。

import {createNamespacedHelpers} from "vuex"
const {mapState,mapActions} from = createNamespacedHelpers('some/nested/module')

export default{
    computed:{
        ...mapState({
            a:state=>state.a,
            b:state=>state.b
        })
    },
    methods:{
        ...mapActions({
            'foo',
            'bar'
        })
    }
}

实现 todoList

参考vue 官网todos 实现

源代码

todosList展示页

store 实现

// store.js

import Vue from "vue";
import Vuex from "vuex";
import todoStorage from "./lib/todoStorage";

Vue.use(Vuex);
const store = new Vuex.Store({
  state: {
    todos: todoStorage.fetch(),
    newTodo: "",
    editedTodo: null,
    visibility: "all",
    loading: false
  },
  getters: {
    activeTodo(state) {
      return state.todos.filter((todo) => !todo.completed);
    },
    completedTodo(state) {
      return state.todos.filter((todo) => todo.completed);
    },
    findTodo(state, getters) {
      if (state.visibility === "all") {
        return state.todos;
      } else if (state.visibility === "active") {
        return getters.activeTodo;
      } else if (state.visibility === "completed") {
        return getters.completedTodo;
      }
    },
    allDone(state, getters) {
      return getters.activeTodo.length === 0;
    },
    remaining(state, getters) {
      return getters.activeTodo.length;
    }
  },
  mutations: {
    addTodo(state, payload) {
      let uid = state.todos.length;
      state.todos.push({
        id: uid++,
        title: payload,
        completed: false
      });
      state.newTodo = "";
      todoStorage.save(state.todos);
    },
    removeTodo(state, payload) {
      state.todos.splice(state.todos.indexOf(payload), 1);
      todoStorage.save(state.todos);
    },
    updateTodo(state, payload) {
      let index = state.todos.findIndex((todo) => todo.id === payload.id);
      if (payload.completed !== undefined) {
        state.todos[index].completed = payload.completed;
      } else if (payload.title !== undefined) {
        state.todos[index].title = payload.title;
      }
      todoStorage.save(state.todos);
    },
    removeCompleted(state) {
      state.todos = state.todos.filter((todo) => !todo.completed);
      todoStorage.save(state.todos);
    },
    updateNewTodo(state, message) {
      state.newTodo = message;
    },
    updateAllDone(state, message) {
      state.allDone = message;
      state.todos.forEach((todo) => (todo.completed = message));
    },
    setLoading(state, payload) {
      state.loading = payload;
    },
    setVisibility(state, payload) {
      state.visibility = payload;
    },
    setEditedTodo(state, message) {
      state.editedTodo = message;
    }
  },
  actions: {
    asyncAddTodo({ commit }, payload) {
      return new Promise((resolve, reject) => {
        commit("setLoading", true);
        setTimeout(() => {
          commit("setLoading", false);
          commit("addTodo", payload);
          resolve();
        }, 1000);
      });
    },
    asyncRemoveTodo({ commit }, payload) {
      return new Promise((resolve, reject) => {
        commit("setLoading", true);
        setTimeout(() => {
          commit("setLoading", false);
          commit("removeTodo", payload);
          resolve();
        }, 1000);
      });
    },
    asyncUpdateTodo({ commit }, payload) {
      return new Promise((resolve, reject) => {
        commit("setLoading", true);
        setTimeout(() => {
          commit("setLoading", false);
          commit("updateTodo", payload);
        }, 1000);
      });
    },
    asyncFindTodo({ commit }, payload) {
      return new Promise((resolve, reject) => {
        commit("setLoading", true);
        setTimeout(() => {
          commit("setLoading", false);
          commit("setVisibility", payload);
        }, 300);
      });
    },
    asyncRemoveCompleted({ commit }) {
      return new Promise((resolve, reject) => {
        commit("setLoading", true);
        setTimeout(() => {
          commit("setLoading", false);
          commit("removeCompleted");
        }, 1000);
      });
    }
  }
});

export default store;

todoStorage 插件

// lib/todoStorage.js

var STORAGE_KEY = "todolist";

var todoStorage = {
  fetch: function () {
    var todos = JSON.parse(localStorage.getItem(STORAGE_KEY) || "[]");
    todos.forEach(function (todo, index) {
      todo.id = index;
    });
    return todos;
  },
  save: function (todos) {
    localStorage.setItem(STORAGE_KEY, JSON.stringify(todos));
  }
};

export default todoStorage;

AllDone 组件

// AllDone
<template>
  <div>
    <input
      id="toggle-all"
      class="toggle-all"
      type="checkbox"
      v-model="allDone"
    />
    <label for="toggle-all"></label>
  </div>
</template>
<script>
export default {
  computed: {
    allDone: {
      get() {
        return this.$store.getters.allDone;
      },
      set(value) {
        this.$store.commit("updateAllDone", value);
      },
    },
  },
};
</script>

Header 组件

// Header
<template>
  <header class="header">
    <h1>todos</h1>
    <input
      class="new-todo"
      autofocus
      autocomplete="off"
      placeholder="What needs to be done?"
      v-model="newTodo"
      @keyup.enter="asyncAddTodo($event.target.value)"
    />
  </header>
</template>
<script>
import { mapActions } from "vuex";
export default {
  name: "header",
  computed: {
    newTodo: {
      get() {
        return this.$store.state.newTodo;
      },
      set(value) {
        this.$store.commit("updateNewTodo", value);
      },
    },
  },
  methods: {
    ...mapActions(["asyncAddTodo"]),
  },
};
</script>

TodoList 组件

// TodoList
<template>
  <section class="main" v-show="todos.length">
    <AllDone />
    <ul class="todo-list">
      <li
        v-for="todo in findTodo"
        class="todo"
        :key="todo.id"
        :class="{ completed: todo.completed, editing: todo == editedTodo }"
      >
        <TodoItem
          :todo="todo"
          :completed.sync="todo.completed"
          :title.sync="todo.title"
        />
      </li>
    </ul>
  </section>
</template>
<script>
import { mapState, mapGetters } from "vuex";
import AllDone from "./AllDone";
import TodoItem from "./TodoItem";
export default {
  name: "todoList",
  components: {
    AllDone,
    TodoItem,
  },
  computed: {
    ...mapState(["todos", "editedTodo"]),
    ...mapGetters(["findTodo"]),
  },
};
</script>

TodoItem组件

// TodoItem
<template>
  <div>
    <div class="view">
      <input
        class="toggle"
        :class="{ checked: completed }"
        type="checkbox"
        :value="completed"
        @change="changeCompleted"
      />
      <label @dblclick="editTodo(todo)">{{ todo.title }}</label>
      <button class="destroy" @click="removeTodo(todo)"></button>
    </div>
    <input
      class="edit"
      type="text"
      :value="title"
      @input="$emit('update:title', $event.target.value)"
      v-todo-focus="todo == editedTodo"
      @blur="doneEdit(todo)"
      @keyup.enter="doneEdit(todo)"
      @keyup.esc="cancelEdit(todo)"
    />
  </div>
</template>
<script>
import { mapState, mapActions } from "vuex";
export default {
  props: ["todo", "completed", "title"],
  data() {
    return {
      beforeEditCache: "",
    };
  },
  computed: {
    ...mapState(["editedTodo"]),
  },
  methods: {
    changeCompleted() {
      this.$emit("update:completed", !this.completed);
    },
    editTodo(todo) {
      this.beforeEditCache = todo.title;
      this.$store.commit("setEditedTodo", todo);
    },
    doneEdit(todo) {
      if (!this.editedTodo) {
        return;
      }

      this.$store.commit("setEditedTodo", null);
      let title = todo.title.trim();
      if (!title) {
        this.removeTodo(todo);
      }
    },
    cancelEdit(todo) {
      this.$store.commit("setEditedTodo", null);
      this.$emit("update:title", this.beforeEditCache);
    },
    ...mapActions({
      removeTodo: "asyncRemoveTodo",
    }),
  },
  directives: {
    "todo-focus": function (el, binding) {
      if (binding.value) {
        el.focus();
      }
    },
  },
};
</script>

Footer 组件

// Footer
<template>
  <footer class="footer" v-show="todos.length">
    <span class="todo-count">
      <strong>{{ remaining }}</strong> {{ remaining | pluralize }} left
    </span>
    <ul class="filters">
      <li v-for="item in types" :key="item.id">
        <a
          href="javascript:;"
          :class="{ selected: visibility == item.type }"
          @click="asyncFindTodo(item.type)"
          >{{ item.type }}</a
        >
      </li>
    </ul>
    <button
      class="clear-completed"
      @click="asyncRemoveCompleted"
      v-show="todos.length > remaining"
    >
      Clear completed
    </button>
  </footer>
</template>
<script>
import { mapState, mapActions, mapGetters } from "vuex";
export default {
  data() {
    return {
      types: [
        {
          id: 1,
          type: "all",
        },
        {
          id: 2,
          type: "active",
        },
        {
          id: 3,
          type: "completed",
        },
      ],
    };
  },
  computed: {
    ...mapState(["todos", "visibility"]),
    ...mapGetters(["activeTodo", "remaining"]),
  },
  filters: {
    pluralize: function (n) {
      return n === 1 ? "item" : "items";
    },
  },
  methods: {
    ...mapActions(["asyncFindTodo", "asyncRemoveCompleted"]),
  },
};
</script>
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,233评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,013评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,030评论 0 241
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,827评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,221评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,542评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,814评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,513评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,225评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,497评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,998评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,342评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,986评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,812评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,560评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,461评论 2 266

推荐阅读更多精彩内容