Common knowledge points in Vuex (summary)
If you have used vue.js before, you must know the pain of passing values between components in vue. In vue, we can use vuex to save the state we need to manage. Value, let's take a look at some commonly used knowledge points in vuex, I hope it will be helpful to everyone.
1. Why use Vuex
1. Multiple components rely on the same state, and using communication methods between components will be very cumbersome, such as multi-layer nested components. .
2. Data that needs to be saved globally, such as user, permission information, global system settings
2. Five core attributes of Vuex
1 , state: storage state
// store.jsconst store = new Vuex.Store({ state: { count: 0 }});// 组件里获取count值$store.state.count
2. getters: state is used as the first parameter, and other getters are used as the second parameter. The return value will be cached according to its dependencies, which is equivalent to Vue’s calculated properties
// store.jsconst store = new Vuex.Store({ state: { count: 1, sum: 2 }, getters: { getCountAndSum: (state,getters) => { return state.count + state.sum; } }});// 其他组件获取getter$store.getters.getCountAndSum
3. Mutations: Modify the state (synchronous), state as the first parameter, submit the payload as the second parameter
const store = new Vuex.Store({ state: { count: 1 }, mutations: { increment (state, obj) { state.count += obj.n; } }});// 其他组件调用mutations的方法$store.commit('increment', {n: 100});
4. Actions: Asynchronous operations (methods for executing mutations, not direct Change status)
const store = new Vuex.Store({ state: { count: 1 }, mutations: { increment (state, obj) { state.count += obj.n; } }, actions: { increment (context) { context.commit('increment'); } }});// 其他组件调用actions的方法$store.dispatch('increment');
5. modules: submodule of store
const a = { state: { count: 0 }, getters: { getCount2 (state, getters, rootState) { return state.count + 2; } }, mutations: { increment (state, getters, rootState) { state.count++; } }, actions: { increment (context) { // context.state.count; // context.rootState.count; context.commit('increment'); } }};const b = {};const store = new Vuex.Store({ modules: { a, b }});// 其他组件调用 (获取state的变量需要加上引入的module的别名)$store.state.a$store.state.b
3. Vuex auxiliary function
<template> <div class="about"> <h1>count: <span>{{count}}</span></h1> <h1>getCount: <span>{{$store.getters.getCount}}</span></h1> <h1>sum: <span>{{sum}}</span></h1> <h1>getSum: <span>{{$store.getters.getSum}}</span></h1> <button @click="clickB">test </button> </div></template><script>import {mapState, mapGetters, mapMutations, mapActions} from 'vuex'; export default { name: 'about', data () { return { count: 0, sum: 0 } }, computed: { ...mapState({ count: state => state.count, countAlias: 'count', countPlusLocalState (state) { return state.count + this.localCount; } }), ...mapGetters([ 'getCount', 'getSum' ]) }, mounted () { this.count = this.$store.state.count; this.sum = this.$store.state.a.sum; }, methods:{ ...mapMutations( 'add','addO' ), ...mapActions([ 'add','addO' ]), clickB () { this.$store.dispatch('add'); this.$store.dispatch('addO'); } } }</script>
Related recommendations:
2020 Summary of front-end vue interview questions (with answers)
vue tutorial recommendation: The latest 5 vue.js video tutorials in 2020 Select
For more programming-related knowledge, please visit:Introduction to Programming! !
The above is the detailed content of Common knowledge points in Vuex (summary). For more information, please follow other related articles on the PHP Chinese website!

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

There are three ways to refer to JS files in Vue.js: directly specify the path using the <script> tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses <router-link to="/" component window.history.back(), and the method selection depends on the scene.

Implement marquee/text scrolling effects in Vue, using CSS animations or third-party libraries. This article introduces how to use CSS animation: create scroll text and wrap text with <div>. Define CSS animations and set overflow: hidden, width, and animation. Define keyframes, set transform: translateX() at the beginning and end of the animation. Adjust animation properties such as duration, scroll speed, and direction.

You can query the Vue version by using Vue Devtools to view the Vue tab in the browser's console. Use npm to run the "npm list -g vue" command. Find the Vue item in the "dependencies" object of the package.json file. For Vue CLI projects, run the "vue --version" command. Check the version information in the <script> tag in the HTML file that refers to the Vue file.

Pagination is a technology that splits large data sets into small pages to improve performance and user experience. In Vue, you can use the following built-in method to paging: Calculate the total number of pages: totalPages() traversal page number: v-for directive to set the current page: currentPage Get the current page data: currentPageData()
