Detailed explanation of the steps for getting started with vuex
This time I will bring you a detailed explanation of the steps for getting started with vuex. What are the precautions for getting started with vuex? The following is a practical case, let’s take a look.
Preface
In the previous projects, we have more or less encountered some places where communication between components is required, but for various reasons,## The cost of #event bus is higher than that of vuex, so I chose vuex for technical selection. But I don’t know why, some newcomers in theteam started to shrink back when they heard about vuex, because vuex is difficult? Really? Is it difficult?
This is purely personal experience, and there are bound to be inaccuracies. If you find any, please correct me. !
Today we use 3 simple steps to prove how simple vuex is.
This is an entry-level tutorial, entry-level tutorial, entry-level tutorial for novices
Step 0Create a new vue project, Install vuex. I won’t introduce too much here. If you can click in, you will have these skills by default ^_^
First stepCreate a new .js file. The location of the name is arbitrary. According to convention, it is recommended to be in the /src/store directory (if not, create a new one yourself)
File location/src/store/index.js
// 引入vue 和 vuex import Vue from 'vue' import Vuex from 'vuex' // 这里需要use一下,固定写法,记住即可 Vue.use(Vuex) // 直接导出 一个 Store 的实例 export default new Vuex.Store({ // 类似 vue 的 data state: { name: 'oldName' }, // 类似 vue 里的 mothods(同步方法) mutations: { updateName (state) { state.name = 'newName' } } })
The code looks a little bit It's a little bit more, but does it look familiar? It's not much different from ordinary vue.
This step is actually to create a new store, but we haven't used it in the project yet.
Second stepIntroduce the above file in the
entry file, and slightly change the parameters passed to new Vue(). There are remarks after the new lines. File location/src/main.js (vue-cli automatically generates the entry, if you can do without scaffolding, then there is no need for me to explain)
import Vue from 'vue' import App from './App' import vuexStore from './store' // 新增 new Vue({ el: '#app', store:vuexStore // 新增 components: { App }, template: '<app></app>' })Tip: The address after import store from './store' is the location of the file we created above (/src/store/index.js).
Because this is index.js, I can omit it.The third step
The above two steps have actually completed the basic configuration of vuex. The next step is to use the
file location/src/main .js (it is also app.vue generated by vue-cli. For the convenience of demonstration, I have removed the redundant code)
<template> <p> {{getName}} <button>更名</button> </p> </template> <script> import HelloWorld from './components/HelloWorld' export default { computed:{ getName(){ return this.$store.state.name } }, methods:{ changeName () { this.$store.commit('updateName') } } } </script>
This is a very ordinary vue file. The difference is that here we need Use the computed attribute to get the "data" in the store
And if we want to change the data, we no longer use this.xxx = xxx and change it to this.$store.commit('updateName')
SummaryYou may think, what is the significance of the above example? Why not just use vue’s data and methods?
The above example is just for simplicity Explain how to use vuex, so some processes are simplified. Just imagine, if you have a page like this:
has a total of 10 layers of components nested (that is, there are sub-subcomponents inside sub-components, and there are sub-sub-components below them. There are sub-sub-sub-components, and so on for 10 layers)
Then when the data of the last layer component changes, when we want to notify the first layer component, we only need to use this.$store in the lowest layer component. commit(),
Then use the computed attribute on the outermost component to get the corresponding value, and you can update it in real time. There is no need to go through $emit layers.
I believe I read the case in this article You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to use v-model and promise to implement the vue pop-up componentHow to use Vue secondary encapsulation axios plug-inThe above is the detailed content of Detailed explanation of the steps for getting started with vuex. For more information, please follow other related articles on the PHP Chinese website!

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools
