Vuex simple tutorial, vuex simple tutorial

Source: Internet
Author: User

Vuex simple tutorial, vuex simple tutorial

What is Vuex?

Vuex is a centralized state management architecture designed specifically for vue. js. Status? I understand it as a part of the data attribute that needs to be shared with other vue components, which is called the status. In short, it is the attribute that needs to be shared in data.

Managing data between components using vuex

npm i vuex -S

Main. js

import Vue from 'vue'import App from './App.vue'import store from './store.js'new Vue({ store, el: '#app', render: h => h(App)})

Store. js

Import Vue from 'vue 'import Vuex from 'vuex' vue. use (Vuex) // The Initial Value let state = {count: 10}; const mutations = {add (context) {context. count ++}, decrease (context) {context. count -- }}; // The logical operation triggered by the event // The parameter is const actions = {add (add) {add. commit ('add')}, decrease (decrease) {decrease. commit ('crease')}, oddAdd ({commit, state}) {if (state. count % 2 = 0) {commit ('add') }}; // return the changed value const getters. = {Count (context) {return context. count}, getOdd (context) {return context. count % 2 = 0? 'Even ': 'odd'}; export default new Vuex. Store ({state, mutations, actions, getters })

App. vue

<Template> <div id = "app"> <button @ click = "add"> add </button> <button @ click = "decrease"> decrease </button> <button @ click = "oddAdd"> oddAdd </button> <div >{{ count }}</div> <div >{{ getOdd }}</div> </div>> </template> <script> import {mapGetters, mapActions} from 'vuex' export default {// the calculated value computed: mapGetters (['Count', 'getodd']), // trigger different functions in the event of a click event: mapActions (['add', 'crease ', 'odddadd'])} </script>

GitHub: https://github.com/wmui

Summary

The above is a simple tutorial on vuex introduced by xiaobian. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.