码迷,mamicode.com
首页 > Web开发 > 详细

vue.js之【vuex】

时间:2017-05-20 14:29:01      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:timeout   --   bsp   comm   res   const   src   ++   mis   

vuex

合在一起写Vuex.Store

目录结构:

  | src

    | store.js

引入:

import Vue from ‘vue‘
import Vuex from ‘vuex‘

使用vuex

Vue.use(Vuex);

定义一个state

var state = {
	count: 10
};

mutations

const mutations = {
	increment(state) { //处理状态(数据)变化
		state.count++;
	},
	decrement(state) {
		state.count--;
	}
};

actions:

const actions = {
	increment: ({ //处理你要干什么,异步请求,判断,流程控制
		commit
	}) => {
		commit(‘increment‘)
	},
	decrement: ({
		commit
	}) => {
		commit(‘decrement‘);
	},
	clickOdd: ({
		commit,
		state
	}) => {
		if (state.count % 2 == 0) {
			commit(‘increment‘)
		}
	},
	clickAsync: ({
		commit
	}) => {
		new Promise((resolve) => { //Promise异步
			setTimeout(function() {
				commit(‘increment‘);

				resolve();
			}, 1000);
		});
	}
};

getters

const getters = {
	count(state) {
		return state.count;
	},
	getOdd(state) {
		return state.count % 2 == 0 ? ‘偶数‘ : ‘奇数‘;
	}
};

需要导出Store对象

export default new Vuex.Store({
	state,
	mutations,
	actions,
	getters
});

 


将vuex拆分开来写

目录结构:

  |src

    |store

      | index.js

      | actions.js

      | mutations.js

      | types.js

      | getters.js

 

vue.js之【vuex】

标签:timeout   --   bsp   comm   res   const   src   ++   mis   

原文地址:http://www.cnblogs.com/Abner5/p/6882022.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!