Home >Web Front-end >JS Tutorial >How to operate the state object of vuex
This time I will show you how to operate the statestateobject of vuex. What are the precautionsfor operating the state object of vuex. The following is a practical case. Let’s take a look. .
vuex is a state management mode specially designed for vue.js, and can also be debugged using devtools.
The following is the structure of my vuex.
The following is the content of state.js and index.js under the store folder
//state.js const state = { headerBgOpacity:0, loginStatus:0, count:66 } export default state //index.js import Vue from 'vue' import Vuex from 'vuex' import state from './state' import actions from './actions' import getters from './getters' import mutations from './mutations' Vue.use(Vuex) export default new Vuex.Store({ state, actions, getters, mutations })
Let’s start using the vuex state object in the test.vue component
Method 1
<template> <p class="test"> {{$store.state.count}} <!--第一种方式--> </p> </template> <script type="text/ecmascript-6"> export default{ name:'test', data(){ return{ } } } </script> <style> </style>
Method 2
<template> <p class="test"> {{count}} <!--步骤二--> </p> </template> <script type="text/ecmascript-6"> export default{ name:'test', data(){ return{} }, computed:{ count(){ return this.$store.state.count; //步骤一 } } } </script> <style> </style>
method three
<template> <p class="test"> {{count}} <!--步骤三--> </p> </template> <script type="text/ecmascript-6"> import {mapState} from 'vuex' //步骤一 export default{ name:'test', data(){ return{} }, computed:mapState({ //步骤二,对象方式 count:state => state.count }) } </script> <style> </style>
method four
<template> <p class="test"> {{count}} <!--步骤三--> </p> </template> <script type="text/ecmascript-6"> import {mapState} from 'vuex' //步骤一 export default{ name:'test', data(){ return{} }, computed:mapState([ //步骤二,数组方式 "count" ]) } </script> <style> </style>
method five
<template> <p class="test"> {{count}} <!--步骤三--> </p> </template> <script type="text/ecmascript-6"> import {mapState} from 'vuex' //步骤一 export default{ name:'test', data(){ return{} }, computed:{ ...mapState([ //步骤二,三个点方式 "count" ]) } } </script> <style> </style>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to use Vue to implement a countdown button
How to use Vue to write a two-way data binding
The above is the detailed content of How to operate the state object of vuex. For more information, please follow other related articles on the PHP Chinese website!