[关闭]
@2890594972 2017-10-26T00:45:26.000000Z 字数 1018 阅读 1019

vue全家桶与 vuex 知识

vue


vuex 是什么

解决组件间通讯成本高的问题
原文:Centralized State Management for Vue.js.
翻译:为vue提供集中状态管理

vuex 由什么组成

组成:action + mutations + state

vuex 特点

1、单向数据流
2、想调用mutations,用commit;
想改变state,用mutate
想通过组件交互,改变state,用dispatch

记住:
action: 用于处理异步相关数据操作
mutations: 用于处理同步操作

vuex如何安装

安装

  1. "dependencies": {
  2. "vue": "^2.5.2",
  3. "vue-resource": "^1.3.4",
  4. "vuex":"^3.0.0",
  5. "vue-router": "^3.0.1"
  6. }

vuex如何使用

第一步:引入

  1. import Vuex from 'vuex'

第二步:启用

  1. Vue.use(Vuex);

第三步之前

  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. //暴露对象接口
  5. export default new Vuex.Store({
  6. state: {
  7. title:'1506'
  8. },
  9. mutations:{
  10. autoIncrement(state,param){
  11. console.log(6666666666,this, arguments);
  12. +state.title++
  13. }
  14. }
  15. })

第三步:使用

  1. this.$store

详解state, mutations, action

state
1、双向代理状态数据
2、谁能改变state: mutations

  1. state: {
  2. title:'1506'
  3. }

mutations
怎么调用mutations中的方法
1、this.$store.commit(函数名,自定义参数);
函数:
参数:2个
第一个参数state,
第二是自定义传参
作用域: Store

  1. mutations:{
  2. autoIncrement(state,param){
  3. console.log(6666666666,this, param);
  4. +state.title++
  5. }
  6. }

actions
如何调用?
this.$store.dispatch(函数名,自定义参数);
函数名:
作用域:Store
参数:2个
第一个参数是actions对象,第二个自定参数

  1. actions: {
  2. //triggerMe如何触发
  3. triggerMe(actions,v){
  4. }
  5. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注