Veux的哲学,实质上是人生的哲学。vue
看一看这张图。vuex
想想,人活着不就是如此吗? app
你的灵魂,控制着你的身体。异步
你的身体,与世界进行互动,改变并影响着人间。async
人间所发生的一切,又时时刻刻塑造着你的灵魂。函数
闭上眼睛,跟随本身去体验:你心中起了一个想法,你的身体也随着这个念头去作出行动。 而这个行动,又改造了你周边的世界。同时,你也听见、看见了人间发生的事,你的思想你的灵魂在随之改变。this
咱们看一个小型Vue应用。spa
new Vue({ // 魂(State):你的心中所想 data () { return { money: 0 } }, // 形(View): 你的外在形态 template: ` <div>我有这么多钱:{{ money }}</div> `, // 人间(Action):你的所作所为所见所闻 methods: { earn_money () { this.money++ } } })
现代人彷佛把一切都简化了——只为“钱”。设计
人活着就沦为了这样的东西:3d
money
。<div>我有这么多钱:{{ money }}</div>
earn_money () { this.money++ }
闭上眼睛,跟随本身去体验:你是一个纯粹为了钱的人。 你心中所想,只有钱。 你的外在表现,就是你有多少钱。你的一切行为,都是为了钱。
若是人真的能活得这么纯粹,反却是好事。
但真实的人生,每每更复杂:
当面对捉摸不定的思想、深藏不露的人性、变幻莫测的人间...你须要一套处世哲学。当面对大批量的State,没法直接取值的View,耦合严重的Action的时候,你就须要Vuex了。
人生,就是一个大型应用。
Vuex就是人的处世哲学。
当你的人生乱成一团糟时,你能够试试用Veux的方式,来梳理本身的生活。
State,就至关于你心中在乎的事。
那如何去维护这些State呢? 钱、父母的健康、爱情,并非轻轻松松能够获得的,你须要缕清楚之间的关系。
Vuex把复杂的“人间”,拆解成了行动(Action)与目标(Mutation)。
Mutation,即目标,它必须是同步函数的。 它的功能必须是直截了当的,能够简单到“让XX更多”或"让XX归零"的程度。
Action,即行动,在其中能够包含异步操做(如Ajax获取数据),并组合一个个小目标。
固然,咱们有时提取出一些更“有用”的状态,至关于state的计算参数,即
因此, Action + Mutation + state, 以及dispatch和commit两个函数,就构成了Veux的逻辑。
咱们也能够这样来管理生活
store.js
const store = new Vuex.Store({ state: { money: 10000000, energy: 60, love: 30, parent_health:50 }, mutations: { earn_money (state) { state.money += 1000 }, pay_money (state,payload) { state.money -= payload.money_cost if(state.money < 0) state.money = 0 }, restore_energy(state, payload){ state.energy = state.energy + payload.sleep_hour*10 if(state.energy > 100) state.energy = 100 }, use_energy(state,payload){ state.energy -= payload.energy_cost if(state.energy < 0) state.energy = 0 }, be_romantic(state){ state.love += 10 }, enhance_harmony(state){ state.love += 5 parent_health += 10 } }, actions: { async work({commit}){ commit('use_energy') await wait(8) commit('earn_money') }, send_gift({commit}){ commit('pay_money',{money_cost:10000}) commit('be_romantic') }, async family_walk({commit}){ commit('use_energy',{energy_cost = 10}) await wait(1) commit('enhance_harmony') } async sleep({commit}) { await wait(8) commit('restore_energy') }, async dating({dispatch, state}){ dispatch('send_gift') if(state.love >80){ await dispatch('sleep') } } }, getters(){ location: (state)=>{ return state.money>10000000 ? 4 : state.money>5000000 ? 5: null }, walk_time: (state)=>{ return Math.min(energy, parent_health) } } }) module.exports = store
首先,在app.js中导入vuex
import Vue from 'vue' import App from './components/App.vue' import store from './store' new Vue({ el: '#app', store, render: h => h(App) })
以后,在编写.vue文件时,利用mapState, mapGetters, mapActions, 能够访问到StateGettersActions
注意:
mapState和mapGetters必须在computed中访问,由于它们返回的是对象,因此须要用拓展符...进行展开。
mapActions则是将Action挂载到methods上,也须要用拓展符...进行展开。
<script> import { mapState, mapGetters, mapActions } from 'vuex' export default { computed: { ...mapState(['money','love']), ...mapGetters(['location'] } methods: { ...mapActions(['family_walk','dating']) }, created () { this.$store.dispatch('work') } } </script>
成功学中有两个很重要的概念,叫“目标导向” “阶段性执行”,回头来看,不正是Veux的哲学吗?
Mutation目标导向: 设定简单的目标,改变State
Action阶段性执行: 执行一个个Matation、异步函数、和其它阶段性执行。
如今,你不只彻底理解了Veux的设计哲学,你更懂得了如何管理人生。
你能够问本身三个问题:
而后,行动吧! 只要作正确的事,你所期待的,就必定会发生!