How to understand Vue's simple state management store mode

How to understand Vue's simple state management store mode

Overview

The implementation idea of ​​the store state management mode is very simple, which is to define a store object with a state attribute to store shared data and methods to operate these shared data. In the component, store.state shared data is used as part or all of data. When changing the shared data in the store.state object, the interface provided by the store must be called to change the shared data.

The following is a simple todo-list demo to introduce the store state management mode.

1. Define store.js

//store.js
export const store = {
    state: {
        todos:
            {text: 'Write Chinese homework', done: false},
            {text: 'Do math test', done: false}
        ]
    },
    addTodo(str){
        const obj = {text: str, done: false}
        this.state.todos.push(obj)
    },
    setDone(index){
        this.state.todos[index].done = true
    }
}

2. Components using store.js

//A.vue
<template>
    <div class="A">
        I am component A<ul>
           <li v-for="(todo,index) in todos" 
           :key="index" :class="todo.done?'done':''" @click="setDone(index)">
           {{todo.text}}
           </li>
       </ul>
    </div>
</template>

<script>
import {store} from '../store/store.js'
export default {
    name: 'A',
    data(){
        return store.state
    },
    methods: {
        setDone(index){
            store.setDone(index)
        }
    }
}
</script>

<style scoped>
.A{
    background: red;
    color: white;
    padding: 20px;
}
.A li.done{
    background: green;
}
</style>
//B.vue
<template>
    <div class="B">
        <div>
            I am component B, enter the task in the input box below and add the task in component A</div>
        <input type="text" v-model="text">
        <button @click="addTodo">add todo</button>
    </div>
</template>

<script>
import {store} from '../store/store.js'
export default {
    name: 'B',
    data(){
        return {
            text: ''
        }
    },
    methods:{
        addTodo(){
            if(this.text){
                store.addTodo(this.text)
            }
        }
    }
}
</script>

<style scoped>
.B{
    background: yellow;
    padding: 20px;
}
</style>
//App.vue
<template>
  <div id="app">
    <A />
    <B />
  </div>
</template>

<script>

import A from './components/A.vue'
import B from './components/B.vue'

export default {
  name: 'App',
  components:
    A,
    B
  }
}
</script>

3. Achieve results

It can be seen that the data displayed in component A is added and modified in component B, which is data communication through data sharing. The simple store mode is used in this way.

The above is the details of how to understand the store mode of Vue simple state management. For more information about the store mode of Vue simple state management, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • How to use provide to implement state management in Vue3
  • Vue front-end development auxiliary function state management detailed example
  • The practical process of login status management in the vuex project
  • Quickly master how to get started with Vuex state management in Vue3.0
  • Application of mapState idea in vuex
  • How to use vuex in Vue project
  • Use of vuex namespace
  • Vue state management: using Pinia instead of Vuex

<<:  Cause Analysis and Solution of I/O Error When Deleting MySQL Table

>>:  Ubuntu Basic Tutorial: apt-get Command

Recommend

Navicat for MySQL tutorial

First, you need to download and install Navicat f...

Navicat multiple ways to modify MySQL database password

Method 1: Use the SET PASSWORD command First log ...

How to configure virtual user login in vsftpd

yum install vsftpd [root@localhost etc]# yum -y i...

Detailed explanation of the use of Linux lseek function

Note: If there are any errors in the article, ple...

How to build your own Nexus private server in Linux

This article describes how to build a Nexus priva...

Comprehensive summary of Vue3.0's various listening methods

Table of contents Listener 1.watchEffect 2.watch ...

HTML tag full name and function introduction

Alphabetical DTD: Indicates in which XHTML 1.0 DT...

Problem analysis of using idea to build springboot initializer server

Problem Description Recently, when I was building...

CentOS 8.0.1905 installs ZABBIX 4.4 version (verified)

Zabbix Server Environment Platform Version: ZABBI...

Install and build a server environment of PHP+Apache+MySQL on CentOS

Yum (full name Yellow dog Updater, Modified) is a...

The front-end must know how to lazy load images (three methods)

Table of contents 1. What is lazy loading? 2. Imp...

Detailed explanation of this pointing problem in JavaScript function

this keyword Which object calls the function, and...

How to use JS to parse the excel content in the clipboard

Table of contents Preface 1. Paste Events and Cli...

Example code of CSS layout at both ends (using parent's negative margin)

Recently, during the development process, I encou...