diff --git a/20_脚手架/vue_code/24.src_求和案例_getters、mapState、mapGetters、mapActions、mapMulations/App.vue b/20_脚手架/vue_code/24.src_求和案例_getters、mapState、mapGetters、mapActions、mapMulations/App.vue new file mode 100644 index 0000000..016f708 --- /dev/null +++ b/20_脚手架/vue_code/24.src_求和案例_getters、mapState、mapGetters、mapActions、mapMulations/App.vue @@ -0,0 +1,17 @@ + + + diff --git a/20_脚手架/vue_code/24.src_求和案例_getters、mapState、mapGetters、mapActions、mapMulations/assets/logo.png b/20_脚手架/vue_code/24.src_求和案例_getters、mapState、mapGetters、mapActions、mapMulations/assets/logo.png new file mode 100644 index 0000000..f3d2503 Binary files /dev/null and b/20_脚手架/vue_code/24.src_求和案例_getters、mapState、mapGetters、mapActions、mapMulations/assets/logo.png differ diff --git a/20_脚手架/vue_code/24.src_求和案例_getters、mapState、mapGetters、mapActions、mapMulations/components/Count.vue b/20_脚手架/vue_code/24.src_求和案例_getters、mapState、mapGetters、mapActions、mapMulations/components/Count.vue new file mode 100644 index 0000000..8dd2603 --- /dev/null +++ b/20_脚手架/vue_code/24.src_求和案例_getters、mapState、mapGetters、mapActions、mapMulations/components/Count.vue @@ -0,0 +1,52 @@ + + + + + \ No newline at end of file diff --git a/20_脚手架/vue_code/24.src_求和案例_getters、mapState、mapGetters、mapActions、mapMulations/main.js b/20_脚手架/vue_code/24.src_求和案例_getters、mapState、mapGetters、mapActions、mapMulations/main.js new file mode 100644 index 0000000..abf3e27 --- /dev/null +++ b/20_脚手架/vue_code/24.src_求和案例_getters、mapState、mapGetters、mapActions、mapMulations/main.js @@ -0,0 +1,26 @@ +// 引入Vue +import Vue from "vue"; +// 引入App +import App from "./App"; +// 引入插件 +import vueResource from "vue-resource"; + +import store from './store' + +// 设置Vue +Vue.config.productionTip = false + +// 使用插件 +Vue.use(vueResource) + +// 实例化Vue +new Vue({ + components: { + App + }, + render: h => h(App), + store, + beforeCreate() { + Vue.prototype.$bus = this // 安装全局事件总线 + } +}).$mount('#app') diff --git a/20_脚手架/vue_code/24.src_求和案例_getters、mapState、mapGetters、mapActions、mapMulations/store/index.js b/20_脚手架/vue_code/24.src_求和案例_getters、mapState、mapGetters、mapActions、mapMulations/store/index.js new file mode 100644 index 0000000..e7a1f9a --- /dev/null +++ b/20_脚手架/vue_code/24.src_求和案例_getters、mapState、mapGetters、mapActions、mapMulations/store/index.js @@ -0,0 +1,53 @@ +// 用于创建vue中核心 store +// 引入Vue +import Vue from "vue"; +// 引入vuex +import Vuex from 'vuex' + +// 应用Vuex插件 +Vue.use(Vuex) + +// 准备actions-用于响应组件中的动作,业务逻辑需要写到actions中 +const actions = { + vuexAddOdd(context, value) { + if (context.state.sum % 2) { + context.commit('vuexAdd', value) + } + }, + vuexAddWait(context, value) { + setTimeout(() => { + context.commit('vuexAdd', value) + }, 1000 + ) + } +} +//准备mutations-用于操作数据 +const mutations = { + vuexAdd(state, value) { + state.sum += value + }, + vuexSub(state, value) { + state.sum -= value + } +} +//准备state-用于存储数据 +const state = { + sum: 0, + school: '修仙学院', + subject: '修仙' +} + +// getters 用于对state中的数据进行加工,类似用computed +const getters = { + bigSum(state) { + return state.sum * 10 + } +} + +// 创建并导出store +export default new Vuex.Store({ + actions, + mutations, + state, + getters +})