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!

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.