Vuex: an instance for understanding the usage of state, and a vuexstate instance

Source: Internet
Author: User

Vuex: an instance for understanding the usage of state, and a vuexstate instance

1. What is state?

As mentioned in the previous article,VuexA repository contains many objects. WherestateIs the data source storage location, correspondingVueObjectdata(As mentioned lateractionsAndmutationsCorrespondsmethods).

Answer book storage:stateThe stored data is responsive,VueComponent slavestoreRead data, ifstoreThe data in is changed, and the component dependent on the data is updated. (Here "status" = "data"), that is, data and views are synchronized.

2. Local Status

Obtain: InVueObtain data from components, which can be obtained directly through calculation attributes;

The component can still save the local status: althoughVuexOfStoreA repository makes it easier for us to manage data in the same way, but more code will also become lengthy. Some components have their own data, so we canstateIt is placed on the component itself and used as local data for use by this component. Other components cannot be used.

3. mapState

mapStateThe rolestate Andgetters Map to the current componentcomputed Calculating attributes,this.$store.state.

Example

Import {mapState} from 'vuex' export default {computer: mapState ({count: state => state. count, 'Count' // map this. count is store. state. count })}

Look at the source code

Export function mapState (states) {const res ={} // defines an object normalizeMap (states ). forEach ({key, val}) =>{// normalizeMap () function initializes states data res [key] = function mappedState () {return typeof val = 'function' // determines whether val is a function? Val. call (this, this. $ store. state, this. $ store. getters) // If val is a function, store state and getters are used as parameters, and the return value is the return value of mapped State: this. $ store. state [val]}) return res // returns a function} // initialization method function normalizeMap (map) {return Array. isArray (map) // determines whether the state is an array? Map. map (key => ({key, val: key}) // if it is an array, call the map method to convert each array element to {key, val: key }: object. keys (map ). map (key => ({key, val: map [key]}) // otherwise it is an object. traverse the object and convert every val to val: key}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.