The use of vuex!

Vuex is a state management library officially provided by Vue.js, which is used to manage global state in Vue.js applications. It helps you share and manage data between components, making application state management easier and more maintainable.

The following are the basic steps to use Vuex:

1. **Install and configure Vuex:** First, make sure your project has Vue.js installed. Then, you can install Vuex using npm or yarn.

   ```bash
   npm install vuex
   ```

   In your application entry point file (usually main.js), import and configure Vuex.

import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex);

2. **Create Vuex Store:** In Vuex, all states, mutations, actions, etc. are organized in a store. You need to create a Vuex Store and define the global state of the application.

   const store = new Vuex.Store({
     state: {
       count: 0, // 一个示例状态,你可以根据应用需要添加更多状态
       // ...
     },
     mutations: {
       increment(state) {
         state.count++;
       },
       decrement(state) {
         state.count--;
       },
       // ...
     },
     actions: {
       incrementAsync({ commit }) {
         setTimeout(() => {
           commit('increment');
         }, 1000);
       },
       // ...
     },
   });

3. **Use Vuex in the Vue instance:** Inject the created Vuex Store instance into the Vue instance, so that the entire application can access the state and operation.

 new Vue({
     el: '#app',
     store, // 注入Vuex Store
     // ...
   });

4. **Using Vuex in components:** You can access state and operations in Vuex by using `this.$store` in any component.   

<template>
     <div>
       <p>Count: {
   
   { count }}</p>
       <button @click="increment">Increment</button>
       <button @click="decrement">Decrement</button>
     </div>
   </template>

   <script>
   export default {
     computed: {
       count() {
         return this.$store.state.count;
       },
     },
     methods: {
       increment() {
         this.$store.commit('increment');
       },
       decrement() {
         this.$store.commit('decrement');
       },
     },
   };
   </script>

   In the above code, we get the value of state `count` through `this.$store.state.count`, and call the mutation named `increment` through `this.$store.commit('increment')` to modify the state.

5. **Use Actions to handle asynchronous operations:** Sometimes, you need to perform asynchronous operations (such as API calls) in Vuex, and you can use Actions to handle them.

   ```javascript

const store = new Vuex.Store({
     state: {
       count: 0,
       // ...
     },
     mutations: {
       increment(state) {
         state.count++;
       },
       // ...
     },
     actions: {
       incrementAsync({ commit }) {
         setTimeout(() => {
           commit('increment');
         }, 1000);
       },
       // ...
     },
   });

   Use asynchronous operations in components:   

   ```vue

<template>
     <div>
       <p>Count: {
   
   { count }}</p>
       <button @click="incrementAsync">Increment Async</button>
     </div>
   </template>

   <script>
   export default {
     computed: {
       count() {
         return this.$store.state.count;
       },
     },
     methods: {
       incrementAsync() {
         this.$store.dispatch('incrementAsync');
       },
     },
   };
   </script>


   ```

The above is the basic usage of Vuex. With Vuex, you can easily manage the global state in the Vue.js application, realize data sharing and management between components, and make the application easier to maintain and expand.

Guess you like

Origin blog.csdn.net/qq_58647634/article/details/131846380