Home >Web Front-end >Vue.js >Vuex Module-Introduction to the use of state warehouse partitioning

Vuex Module-Introduction to the use of state warehouse partitioning

藏色散人
藏色散人forward
2022-08-10 16:01:011808browse

vuex composition

vuex mainly consists of the following five parts:

  • State // Storage of variables and data
  • Getter // Similar to computed properties
  • Mutation // The only way to modify state
  • Action // Call Mutation asynchronously
  • Module // Modularize the store

vuex modules use

to create directories

Vuex Module-Introduction to the use of state warehouse partitioning

In this example, I created two store files, which are profile.js and custom.js, a root file index.js

custom.js

const customs = {
    namespaced: true, // 创建命名空间
    state: { // 存储变量
        showAlert: false
    },
    mutations: { // 定义修改state方法
        CHANGESHOW: (state, params) => {
            state.showAlert = !state.showAlert        }
    },
    actions: { // 异步调用mutations
        setShow: ({ commit }) => {
            commit('CHANGESHOW')
        }
    },
    getters: { // 将数据过滤输出
        bodyShow: state => state.showAlert    }}export default customs

profile.js

const profile = {
  namespaced: true,
  state: {
    name: 'common name',
    age: 18,
    bool: false
  },
  mutations: {
    CHANGEMSG: (state, params) => {
      state.name = params    },
    CHANGEAGE: (state, params) => {
      state.name = params    },
    CHANGEBOOL: (state) => {
      state.bool = !state.bool    }
  },
  actions: {
    setName: ({ commit }) => {
      commit('CHANGEMSG', 'Vuex common name')
    },
    setAge: ({ commit }) => {
      commit('CHANGEAGE', 81)
    },
    setBool: ({ commit }) => {
      commit('CHANGEBOOL')
    }
  },
  getters: {
    vuexName: state => state.name,
    vuexAge: state => state.age,
    vuexBool: state => state.bool  }}export default common

index.js

import Vue from 'vue'
import Vuex from 'vuex'

// 引入子store
import profile from './modules/profile'
import customs from './modules/customs'

// Vue.use(Vuex)
const store = new Vuex.Store({
  modules: {
    profile,
    customs
  }
})

export default store // 导出store,以便于后续使用

Use it in the .vue file you need to use. The method is as follows

index.vue

<template>
	<div>
	  name: <h5>{{vuexName}}</h5> <button @click=&#39;setName&#39;>chenge name</button>
      age: <h5>{{vuexAge}}</h5> <button @click=&#39;setAge&#39;>chenge age</button>
      bool: <h5>{{vuexBool}}</h5> <button @click=&#39;setBool&#39;>chenge bool</button>
      <br/>
      
      <span @click=&#39;setShow&#39; style=&#39;display:inline-block;width:200px;height:30px;border:1px solid #999;border-radius:5px;text-align:center;line-height:30px;cursor: pointer;&#39;>click me ,change showAlert</span>
      <em>{{bodyShow}}</em>
	</div>
</template>
<script>
import { mapActions, mapGetters } from &#39;vuex&#39;
export default {
computed: {
    ...mapGetters(&#39;profile&#39;, [&#39;vuexName&#39;, &#39;vuexAge&#39;, &#39;vuexBool&#39;]),
    ...mapGetters(&#39;customs&#39;, [&#39;bodyShow&#39;])
  },
methods: {
    ...mapActions(&#39;customs&#39;, [&#39;setShow&#39;]),
    ...mapActions(&#39;profile&#39;, [&#39;setName&#39;, &#39;setAge&#39;, &#39;setBool&#39;]),
}
</script>
<style>

</style>

app.js

import Vue from &#39;vue&#39;;
import VueRouter from &#39;vue-router&#39;;
// style
import &#39;./../../sass/app.scss&#39;;

// Components
import Main from &#39;./Main.vue&#39;;
import routes from &#39;./routes&#39;;
// store
import store from &#39;./store&#39;;  // 将store挂载到Vue

Vue.use(VueRouter);

const router = new VueRouter({
  routes,
  saveScrollPosition: true,
});

new Vue({ router, store, ...Main }).$mount(&#39;#app&#39;);

Initial rendering ⬇️
Vuex Module-Introduction to the use of state warehouse partitioning
After clicking the button, the rendering ⬇️
Vuex Module-Introduction to the use of state warehouse partitioning
At this point, the module usage process demonstration is completed! [Related recommendations: vue.js video tutorial]

The above is the detailed content of Vuex Module-Introduction to the use of state warehouse partitioning. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete