美文网首页
vuex模块化

vuex模块化

作者: 冰点雨 | 来源:发表于2022-05-30 09:13 被阅读0次
22449fea8ead6f9ef2900c8a8d6fa3b.png

App.vue

<template>
  <div>
    <Count />
    <hr />
    <Person />
  </div>
</template>

<script>
// 引入组件
import Count from './components/Count'
import Person from './components/Person.vue'

export default {
  name: 'App',
  components: { Count, Person },
}
</script>

Count.vue

<template>
  <div>
    <h1>当前求和为:{{ sum }}</h1>
    <h3>当前求和放大10倍为:{{ bigSum }}</h3>
    <h3>我在{{ school }},学习{{ subject }}</h3>
    <h3 style="color: red">person组件的总人数是:{{ personList.length }}</h3>
    <select v-model.number="n">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
    </select>
    <button @click="increment(n)">+</button>
    <button @click="decrement(n)">-</button>
    <button @click="incrementOdd(n)">当前求和为奇数再加</button>
    <button @click="incrementWait(n)">等一等再加</button>
  </div>
</template>

<script>
import { mapActions, mapGetters, mapMutations, mapState } from 'vuex'
export default {
  name: 'Count',
  data() {
    return {
      n: 1, //用户选择的数字
    }
  },
  computed: {
    // 借用mapState生成计算属性,从state中读取数据(数组写法)
    ...mapState('countAbout', ['sum', 'school', 'subject']),
    ...mapState('personAbout', ['personList']),

    // 借用mapGetters生成计算属性,从getters中读取数据(数组写法)
    ...mapGetters('countAbout', ['bigSum']),
  },
  methods: {
    ...mapMutations('countAbout', { increment: 'JIA', decrement: 'JIAN' }),

    /* ************************************************ */
    ...mapActions('countAbout', {
      incrementOdd: 'jiaOdd',
      incrementWait: 'jiaWait',
    }),
  },
  mounted: {},
}
</script>

<style>
button {
  margin-left: 10px;
}
</style>

Person.vue

<template>
  <div>
    <h1>人员列表</h1>
    <h3 style="color: red">count组件的和是:{{ sum }}</h3>
    <h3>列表中第一个人的名字是:{{ firstPersonName }}</h3>
    <input type="text" placeholder="请输入名字" v-model="name" />
    <button @click="add">添加</button>
    <button @click="addPersonWang">添加一个姓王的人</button>
    <button @click="addPersonServe">添加一个人,名字随机</button>
    <ul>
      <li v-for="p in personList" :key="p.id">{{ p.name }}</li>
    </ul>
  </div>
</template>

<script>
import { nanoid } from 'nanoid'
export default {
  name: 'Person',
  computed: {
    personList() {
      return this.$store.state.personAbout.personList
    },
    sum() {
      return this.$store.state.countAbout.sum
    },
    firstPersonName() {
      return this.$store.getters['personAbout/firstPersonName']
    },
  },
  methods: {
    add() {
      const personObj = { id: nanoid(), name: this.name }
      this.$store.commit('personAbout/ADD_PERSON', personObj)
      this.name = ''
    },
    addPersonWang() {
      const personObj = { id: nanoid(), name: this.name }
      this.$store.dispatch('personAbout/addPersonWang', personObj)
      this.name = ''
    },
    addPersonServe() {
      this.$store.dispatch('personAbout/addPersonServer')
    },
  },
}
</script>

<style scoped></style>

index.js

// 该文件用于创建Vuex中最为核心的store
import Vue from 'vue'
//引入vuex
import Vuex from "vuex";
import countOptions from './count'
import personOptions from './person'
Vue.use(Vuex)

//暴露、创建store
export default new Vuex.Store({
  modules: {
    countAbout:countOptions,
    personAbout:personOptions
  }
})

count.js(只写count相关逻辑)

//求和功能相关的配置
export default {
  namespaced:true,
  actions: { 
    jiaOdd(context,value){
  //  console.log("actions中的jian调用了")
      context.commit('JIAODD', value)
    },
    jiaWait (context, value) {
  //  console.log("actions中的jian调用了")
      context.commit('JIAWAIT', value)
    }
  },
  mutations: {
     JIA(state,value){
      //  console.log("mutations中的jia调用了")
      state.sum += value
    },
    JIAN (state, value) {
      //  console.log("mutations中的jia调用了")
      state.sum -= value
    },
    JIAODD (state, value) {
      //  console.log("mutations中的jia调用了")
      if (state.sum % 2) {
        state.sum += value
      }
    },
    JIAWAIT (state, value) {
      //  console.log("mutations中的jia调用了")
      setTimeout(() => {
        state.sum += value
      }, 500);
    },
  },
  state: {
     sum: 0,//当前的和
     school: '清华大学',
     subject: '前端',
  },
  getters: {
    bigSum (state) {
      return state.sum * 10
    }
  }
}

person.js(只写person相关逻辑)

import axios from "axios"
import nanoid from "nanoid"
//人员管理相关的配置
export default {
  namespaced:true,
  actions: {
    addPersonWang (context, value) {
      if (value.name.indexOf('王') === 0) {
        context.commit('ADD_PERSON',value)
      } else {
        alert('添加的人必须姓王')
      }
    },
    addPersonServer (context) {
      axios.get('https://api.uixsj.cn/hitokoto/get?type=social').then(
        response => {
          context.commit('ADD_PERSON',{ id: nanoid(), name: response.data })
        },
        error => {
          alert(error.message)
        }
      )
    }
   },
  mutations: {
    ADD_PERSON (state, value) {
      state.personList.unshift(value)
    }
  },
  state: {
    personList: [
      { id: '001', name: '张三' },
    ]
  },
  getters: {
    firstPersonName (state) {
      return state.personList[0].name
    }
  }
}

相关文章

  • 模块化vuex,获取、设置数据,及刷新保留数据方法

    前言 1.模块化vuex2.获取vuex中的数据3.设置vuex中的数据4.刷新之后,保留数据 模块化vuex 数...

  • VUEX模块化

    vuex模块化官方文档 项目github地址 在vuex的官网上,关于模块化和热重载的文档都是蛮详细的,这边也就不...

  • vuex相关文章

    vuex模块化和命名空间的实例代码

  • vuex持久化+模块化实战用法(进阶篇)

    上一篇:vuex刷新数据消失不见解决方案 Vuex模块化 模块化后的 store 大概长这样,如果画的不对,欢迎留...

  • vuex入门实例2

    vuex入门实例2 这章节我准备以requirejs模块化,讲解下如何快速上手vuex 源码地址 工程代码结构如下...

  • 2018-03-13

    Vuex 进阶——模块化组织 Vuex 前两篇讲解了一下 Vuex 的基本使用方法,可是在实际项目中那么写肯定是不...

  • vuex模块化

    上一节[https://www.jianshu.com/p/5dd561f3570d] modules modul...

  • vuex模块化

    demo store/modules/user.js 可以通过添加 namespaced: true 的方式使其成...

  • vuex模块化

    App.vue Count.vue Person.vue index.js count.js(只写count相关逻...

  • Vuex模块化

    目录 配置 在src目录下新建store文件夹,内部新建add.js、cut.js和index.js 其中主体的配...

网友评论

      本文标题:vuex模块化

      本文链接:https://www.haomeiwen.com/subject/ntpjdrtx.html