Table of Contents
vuex composition
vuex modules use
to create directories
{{vuexName}}
{{vuexAge}}
{{vuexBool}}
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

Aug 10, 2022 pm 04:01 PM
vuex module

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
Copy after login

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
Copy after login

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,以便于后续使用
Copy after login

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

index.vue

<template>
	<div>
	  name: <h5 id="vuexName">{{vuexName}}</h5> <button @click=&#39;setName&#39;>chenge name</button>
      age: <h5 id="vuexAge">{{vuexAge}}</h5> <button @click=&#39;setAge&#39;>chenge age</button>
      bool: <h5 id="vuexBool">{{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>
Copy after login

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;);
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Best practices for using Vuex to manage global state in Vue2.x Best practices for using Vuex to manage global state in Vue2.x Jun 09, 2023 pm 04:07 PM

Vue2.x is one of the most popular front-end frameworks currently, which provides Vuex as a solution for managing global state. Using Vuex can make state management clearer and easier to maintain. The best practices of Vuex will be introduced below to help developers better use Vuex and improve code quality. 1. Use modular organization state. Vuex uses a single state tree to manage all the states of the application, extracting the state from the components, making state management clearer and easier to understand. In applications with a lot of state, modules must be used

ModuleNotFoundError: How to solve Python module not found error? ModuleNotFoundError: How to solve Python module not found error? Jun 25, 2023 pm 09:30 PM

During the development process of Python, we often encounter module not found errors. The specific manifestation of this error is that Python reports one of two errors: ModuleNotFoundError or ImportError when importing the module. This error is very annoying and can cause the program to not run properly, so in this article, we will explore the causes of this error and how to solve it. ModuleNotFoundError and ImportError in Pyth

How to solve the problem 'Error: [vuex] do not mutate vuex store state outside mutation handlers.' when using vuex in a Vue application? How to solve the problem 'Error: [vuex] do not mutate vuex store state outside mutation handlers.' when using vuex in a Vue application? Jun 24, 2023 pm 07:04 PM

In Vue applications, using vuex is a common state management method. However, when using vuex, we may sometimes encounter such an error message: "Error:[vuex]donotmutatevuexstorestateoutsidemutationhandlers." What does this error message mean? Why does this error message appear? How to fix this error? This article will cover this issue in detail. The error message contains

How to use Vuex in Vue3 How to use Vuex in Vue3 May 14, 2023 pm 08:28 PM

What does Vuex do? Vue official: State management tool What is state management? State that needs to be shared among multiple components, and it is responsive, one change, all changes. For example, some globally used status information: user login status, user name, geographical location information, items in the shopping cart, etc. At this time, we need such a tool for global status management, and Vuex is such a tool. Single-page state management View–>Actions—>State view layer (view) triggers an action (action) to change the state (state) and responds back to the view layer (view) vuex (Vue3.

Java9 new feature Module modular programming method Java9 new feature Module modular programming method May 19, 2023 pm 01:51 PM

In the Java9 version, the Java language introduced a very important concept: module. If you are familiar with the modular management of JavaScript code, you should feel familiar when you see the modular management of Java 9. 1. What is Javamodule? Somewhat similar to packages in Java, modules introduce another level of grouping of Java code. Each such group (module) contains many sub-packages. Declare the folder and its subfolders as a module by adding the file module-info.java to the root of a module's source code file package. The file syntax

How to solve the problem 'Error: [vuex] unknown action type: xxx' when using vuex in a Vue application? How to solve the problem 'Error: [vuex] unknown action type: xxx' when using vuex in a Vue application? Jun 25, 2023 pm 12:09 PM

In Vue.js projects, vuex is a very useful state management tool. It helps us share state among multiple components and provides a reliable way to manage state changes. But when using vuex, sometimes you will encounter the error "Error:[vuex]unknownactiontype:xxx". This article will explain the cause and solution of this error. 1. Cause of the error When using vuex, we need to define some actions and mu

Learn more about the implementation principles of vuex Learn more about the implementation principles of vuex Mar 20, 2023 pm 06:14 PM

When asked in an interview about the implementation principle of vuex, how should you answer? The following article will give you an in-depth understanding of the implementation principle of vuex. I hope it will be helpful to you!

How to solve the problem 'TypeError: Cannot read property 'xxx' of undefined' when using vuex in Vue application? How to solve the problem 'TypeError: Cannot read property 'xxx' of undefined' when using vuex in Vue application? Aug 18, 2023 pm 09:24 PM

Using Vuex in Vue applications is a very common operation. However, occasionally when using Vuex, you will encounter the error message "TypeError: Cannotreadproperty'xxx'ofundefined". This error message means that the undefined property "xxx" cannot be read, resulting in a program error. The reason for this problem is actually very obvious. It is because when calling a certain attribute of Vuex, this attribute is not correctly set.

See all articles