From b86b7e3a669cfcb090b528a5ea4f04d34dbe83a8 Mon Sep 17 00:00:00 2001 From: sw_pc Date: Tue, 12 Sep 2023 19:38:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E7=BB=84=E4=BB=B6=E5=85=B1=E4=BA=AB?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- course/vue2/vuex/heima/src/App.vue | 30 +++++--- .../vue2/vuex/heima/src/components/Count2.vue | 44 ++++++++++++ .../vue2/vuex/heima/src/components/Person.vue | 38 +++++++++++ course/vue2/vuex/heima/src/store/index.js | 68 ++++++++++++------- 4 files changed, 145 insertions(+), 35 deletions(-) create mode 100644 course/vue2/vuex/heima/src/components/Count2.vue create mode 100644 course/vue2/vuex/heima/src/components/Person.vue diff --git a/course/vue2/vuex/heima/src/App.vue b/course/vue2/vuex/heima/src/App.vue index 53492b4..3a45a60 100644 --- a/course/vue2/vuex/heima/src/App.vue +++ b/course/vue2/vuex/heima/src/App.vue @@ -1,22 +1,30 @@ diff --git a/course/vue2/vuex/heima/src/components/Count2.vue b/course/vue2/vuex/heima/src/components/Count2.vue new file mode 100644 index 0000000..adf0bb9 --- /dev/null +++ b/course/vue2/vuex/heima/src/components/Count2.vue @@ -0,0 +1,44 @@ + + + + + diff --git a/course/vue2/vuex/heima/src/components/Person.vue b/course/vue2/vuex/heima/src/components/Person.vue new file mode 100644 index 0000000..39bcdc7 --- /dev/null +++ b/course/vue2/vuex/heima/src/components/Person.vue @@ -0,0 +1,38 @@ + + + diff --git a/course/vue2/vuex/heima/src/store/index.js b/course/vue2/vuex/heima/src/store/index.js index 0efce32..1a79f84 100644 --- a/course/vue2/vuex/heima/src/store/index.js +++ b/course/vue2/vuex/heima/src/store/index.js @@ -1,29 +1,12 @@ +//引入Vue核心库 import Vue from 'vue' +//引入Vuex import Vuex from 'vuex' - +//应用Vuex插件 Vue.use(Vuex) - -export default new Vuex.Store({ - state: { - sum:0 //当前的和 - }, - getters: { - bigSum(state) { - console.log(state) - return state.sum * 10 - } - }, - mutations: { - ADD(state,value){ - console.log("mutations ADD") - state.sum += value - }, - SUBTRACT(state,value){ - console.log("mutations SUBTRACT") - state.sum -= value - } - }, - actions: { + +//准备actions对象——响应组件中用户的动作 +const actions = { addOdd(context,value){ console.log("actions中的addOdd被调用了") if(context.state.sum % 2){ @@ -36,7 +19,40 @@ export default new Vuex.Store({ context.commit('ADD',value) },500) }, - }, - modules: { - } +} +//准备mutations对象——修改state中的数据 +const mutations = { + ADD(state,value){ + state.sum += value + }, + SUBTRACT(state,value){ + state.sum -= value + }, + ADD_PERSON(state,value){ + console.log('mutations中的ADD_PERSON被调用了') + state.personList.unshift(value) + } +} +//准备state对象——保存具体的数据 +const state = { + sum:0, //当前的和 + name:'JOJO', + school:'尚硅谷', + personList:[ + {id:'001',name:'JOJO'} + ] +} +//准备getters对象——用于将state中的数据进行加工 +const getters = { + bigSum(){ + return state.sum * 10 + } +} + +//创建并暴露store +export default new Vuex.Store({ + actions, + mutations, + state, + getters }) -- GitLab