diff --git a/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/App.vue b/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/App.vue new file mode 100644 index 0000000..3dc2784 --- /dev/null +++ b/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/App.vue @@ -0,0 +1,19 @@ + + + diff --git a/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/assets/logo.png b/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/assets/logo.png new file mode 100644 index 0000000..f3d2503 Binary files /dev/null and b/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/assets/logo.png differ diff --git a/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/components/Count.vue b/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/components/Count.vue new file mode 100644 index 0000000..60fffa8 --- /dev/null +++ b/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/components/Count.vue @@ -0,0 +1,53 @@ + + + + + \ No newline at end of file diff --git a/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/components/Person.vue b/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/components/Person.vue new file mode 100644 index 0000000..1f7986f --- /dev/null +++ b/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/components/Person.vue @@ -0,0 +1,45 @@ + + + + + \ No newline at end of file diff --git a/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/main.js b/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/main.js new file mode 100644 index 0000000..abf3e27 --- /dev/null +++ b/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/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/25.src_求和案例_多组件共享数据/store/index.js b/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/store/index.js new file mode 100644 index 0000000..1decd27 --- /dev/null +++ b/20_脚手架/vue_code/25.src_求和案例_多组件共享数据/store/index.js @@ -0,0 +1,59 @@ +// 用于创建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 + }, + ADD_PERSON(state, personObj) { + state.personList.unshift(personObj) + } +} +//准备state-用于存储数据 +const state = { + sum: 0, + school: '修仙学院', + subject: '修仙', + personList: [ + {id:'001', name:'张三'} + ] +} + +// getters 用于对state中的数据进行加工,类似用computed +const getters = { + bigSum(state) { + return state.sum * 10 + } +} + +// 创建并导出store +export default new Vuex.Store({ + actions, + mutations, + state, + getters +}) diff --git a/20_脚手架/vue_code/src/App.vue b/20_脚手架/vue_code/src/App.vue index 016f708..3dc2784 100644 --- a/20_脚手架/vue_code/src/App.vue +++ b/20_脚手架/vue_code/src/App.vue @@ -1,15 +1,17 @@ + + \ No newline at end of file diff --git a/20_脚手架/vue_code/src/store/index.js b/20_脚手架/vue_code/src/store/index.js index e7a1f9a..1decd27 100644 --- a/20_脚手架/vue_code/src/store/index.js +++ b/20_脚手架/vue_code/src/store/index.js @@ -28,13 +28,19 @@ const mutations = { }, vuexSub(state, value) { state.sum -= value + }, + ADD_PERSON(state, personObj) { + state.personList.unshift(personObj) } } //准备state-用于存储数据 const state = { sum: 0, school: '修仙学院', - subject: '修仙' + subject: '修仙', + personList: [ + {id:'001', name:'张三'} + ] } // getters 用于对state中的数据进行加工,类似用computed