search
HomeWeb Front-endJS TutorialAbout Vuex's family bucket status management
About Vuex's family bucket status managementMay 21, 2018 am 09:13 AM
vuexstatemanage

Vuex is a state management pattern developed specifically for Vue.js applications. It uses centralized storage to manage the state of all components of the application, and uses corresponding rules to ensure that the state changes in a predictable way. Vuex is also integrated into Vue's official debugging tool devtools extension, which provides advanced debugging functions such as zero-configuration time-travel debugging, state snapshot import and export, etc. This article mainly introduces a brief discussion of Vuex state management (Family Bucket). The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

The above is the introduction of vuex in the official document of vuex. The official document explains the usage of vuex in detail. I won’t go into details about the usage of vuex here. The purpose of writing this blog is just to help some students understand and get started with vuex faster.

1. Install

$ npm install vuex --save

2. Reference store.js in main.js main entrance js

import Vue from 'vue'
import App from './App'
import router from './router' 
import store from './vuex/store'  //引用store.js
Vue.config.productionTip = false //阻止在启动时生成生产提示 

//vue实例
new Vue({
 el: '#app',
 router,
 store,              //把store挂在到vue的实例下面
 template: &#39;<App/>&#39;,
 components: { App }
})

3. Reference Vuex

import Vue from &#39;vue&#39;
import Vuex from &#39;vuex&#39;
Vue.use(Vuex) //注册Vuex

// 定义常量  如果访问他的话,就叫访问状态对象
const state = {
  count: 1
}

// mutations用来改变store状态, 如果访问他的话,就叫访问触发状态
const mutations = {
  //这里面的方法是用 this.$store.commit(&#39;jia&#39;) 来触发
  jia(state){
    state.count ++
  },
  jian(state){
    state.count --
  },
}
//暴露到外面,让其他地方的引用
export default new Vuex.Store({
  state,
  mutations
})
in store.js

4. Use in the vue component

Use the $store.commit('jia') area to trigger the addition and subtraction methods under mutations

<template>
 <p class="hello">
   <h1 id="Hello-nbsp-Vuex">Hello Vuex</h1>
   <h5 id="store-state-count">{{$store.state.count}}</h5>
   <p>
    <button @click="$store.commit(&#39;jia&#39;)">+</button>
    <button @click="$store.commit(&#39;jian&#39;)">-</button>
   </p>
 </p>
</template>

<!-- 加上scoped是css只在这个组件里面生效,为了不影响全局样式 -->
<style scoped>
  h5{
   font-size: 20px;
   color: red;
  }
</style>

5. View the demo

6. State access state object

Use computed calculation

<template>
 <p class="hello">
   <h1 id="Hello-nbsp-Vuex">Hello Vuex</h1>
   <h5 id="count">{{count}}</h5>
   <p>
    <button @click="$store.commit(&#39;jia&#39;)">+</button>
    <button @click="$store.commit(&#39;jian&#39;)">-</button>
   </p>
 </p>
</template>

<script>
import {mapState} from &#39;vuex&#39;
export default{
  name:&#39;hello&#39;, //写上name的作用是,如果你页面报错了,他会提示你是那个页面报的错,很实用
  // 方法一
  // computed: {
  //  count(){
  //   return this.$store.state.count + 6
  //  }
  // }
  
  // 方法二 需要引入外部 mapState
  computed:mapState({
   count:state => state.count + 10
  })
 
  // ECMA5用法
  // computed:mapState({
  //  count:function(state){
  //   return state.count
  //  }
  // })
  
  //方法三
  // computed: mapState([
  //  &#39;count&#39;
  // ])
 }
</script>

7. Mutations trigger state (synchronization state)


<template>
 <p class="hello">
   <h1 id="Hello-nbsp-Vuex">Hello Vuex</h1>
   <h5 id="count">{{count}}</h5>
   <p>
    <button @click="jia">+</button>
    <button @click="jian">-</button>
   </p>
 </p>
</template>
<script>
import {mapState,mapMutations} from &#39;vuex&#39;
 export default{
  name:&#39;hello&#39;, //写上name的作用是,如果你页面报错了,他会提示你是那个页面报的错,很实用
  //方法三
  computed: mapState([
   &#39;count&#39;
  ]),
  methods:{
   ...mapMutations([
     &#39;jia&#39;,
     &#39;jian&#39;
   ])
  }
 }
</script>

8. Getters computed properties

Getters cannot use arrow functions, which will change the point of this

Add getters in store.js

// 计算
const getters = {
  count(state){
    return state.count + 66
  }
}

export default new Vuex.Store({
  state,
  mutations,
  getters
})
//count的参数就是上面定义的state对象
//getters中定义的方法名称和组件中使用的时候一定是一致的,定义的是count方法,使用的时候也用count,保持一致。
组件中使用

<script>
 import {mapState,mapMutations,mapGetters} from &#39;vuex&#39;
 export default{
  name:&#39;hello&#39;,
  computed: {
   ...mapState([
    &#39;count&#39;
   ]),
   ...mapGetters([
    &#39;count&#39;
   ])
  },
  methods:{
   ...mapMutations([
     &#39;jia&#39;,
     &#39;jian&#39;
   ])
  }
 }
</script>

9. actions (asynchronous state)

Add actions in store.js

import Vue from &#39;vue&#39;
import Vuex from &#39;vuex&#39;
Vue.use(Vuex)

// 定义常量
const state = {
  count: 1
}

// mutations用来改变store状态 同步状态
const mutations = {
  jia(state){
    state.count ++
  },
  jian(state){
    state.count --
  },
}
// 计算属性
const getters = {
  count(state){
    return state.count + 66
  }
}
// 异步状态
const actions = {
  jiaplus(context){
    context.commit(&#39;jia&#39;) //调用mutations下面的方法
    setTimeout(()=>{
      context.commit(&#39;jian&#39;)
    },2000)
    alert(&#39;我先被执行了,然后两秒后调用jian的方法&#39;)
  },
  jianplus(context){
    context.commit(&#39;jian&#39;)
  }
}

export default new Vuex.Store({
  state,
  mutations,
  getters,
  actions
})

Use in components

<template>
 <p class="hello">
   <h1 id="Hello-nbsp-Vuex">Hello Vuex</h1>
   <h5 id="count">{{count}}</h5>
   <p>
    <button @click="jia">+</button>
    <button @click="jian">-</button>
   </p>
   <p>
    <button @click="jiaplus">+plus</button>
    <button @click="jianplus">-plus</button>
   </p>
 </p>
</template>
<script>
 import {mapState,mapMutations,mapGetters,mapActions} from &#39;vuex&#39;
 export default{
  name:&#39;hello&#39;,
  computed: {
   ...mapState([
    &#39;count&#39;
   ]),
   ...mapGetters([
    &#39;count&#39;
   ])
  },
  methods:{
   // 这里是数组的方式触发方法
   ...mapMutations([
     &#39;jia&#39;,
     &#39;jian&#39;
   ]),
   // 换一中方式触发方法 用对象的方式
   ...mapActions({
    jiaplus: &#39;jiaplus&#39;,
    jianplus: &#39;jianplus&#39;
   })
  }
 }
</script>

<style scoped>
  h5{
   font-size: 20px;
   color: red;
  }
</style>

10. modules module

Applicable to Use it when a very large project has a lot of status and is easy to manage

Modify store.js

import Vue from &#39;vue&#39;
import Vuex from &#39;vuex&#39;
Vue.use(Vuex)

const state = {
  count: 1
}
const mutations = {
  jia(state){
    state.count ++
  },
  jian(state){
    state.count --
  },
}
const getters = {
  count(state){
    return state.count + 66
  }
}
const actions = {
  jiaplus(context){
    context.commit(&#39;jia&#39;) //调用mutations下面的方法
    setTimeout(()=>{
      context.commit(&#39;jian&#39;)
    },2000)
    alert(&#39;我先被执行了,然后两秒后调用jian的方法&#39;)
  },
  jianplus(context){
    context.commit(&#39;jian&#39;)
  }
}

//module使用模块组的方式 moduleA
const moduleA = {
  state,
  mutations,
  getters,
  actions
}

// 模块B moduleB
const moduleB = {
  state: {
    count:108
  }
}

export default new Vuex.Store({
  modules: {
    a: moduleA,
    b: moduleB,
  }
})

Related recommendations:

Example sharing Vue Family Bucket practical project summary

Using React Family Bucket to build a backend management system example detailed explanation

Vue2.0 Web application developed by FamilyBucket (refer to Wuji APP)

The above is the detailed content of About Vuex's family bucket status management. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
在Vue应用中使用vuex时出现“Error: [vuex] do not mutate vuex store state outside mutation handlers.”怎么解决?在Vue应用中使用vuex时出现“Error: [vuex] do not mutate vuex store state outside mutation handlers.”怎么解决?Jun 24, 2023 pm 07:04 PM

在Vue应用中,使用vuex是常见的状态管理方式。然而,在使用vuex时,我们有时可能会遇到这样的错误提示:“Error:[vuex]donotmutatevuexstorestateoutsidemutationhandlers.”这个错误提示是什么意思呢?为什么会出现这个错误提示?如何解决这个错误?本文将详细介绍这个问题。错误提示的含

Vue3中Vuex怎么使用Vue3中Vuex怎么使用May 14, 2023 pm 08:28 PM

Vuex是做什么的?Vue官方:状态管理工具状态管理是什么?需要在多个组件中共享的状态、且是响应式的、一个变,全都改变。例如一些全局要用的的状态信息:用户登录状态、用户名称、地理位置信息、购物车中商品、等等这时候我们就需要这么一个工具来进行全局的状态管理,Vuex就是这样的一个工具。单页面的状态管理View&ndash;>Actions&mdash;>State视图层(view)触发操作(action)更改状态(state)响应回视图层(view)vuex(Vue3.

Vue2.x中使用Vuex管理全局状态的最佳实践Vue2.x中使用Vuex管理全局状态的最佳实践Jun 09, 2023 pm 04:07 PM

Vue2.x是目前最流行的前端框架之一,它提供了Vuex作为管理全局状态的解决方案。使用Vuex能够使得状态管理更加清晰、易于维护,下面将介绍Vuex的最佳实践,帮助开发者更好地使用Vuex以及提高代码质量。1.使用模块化组织状态Vuex使用单一状态树管理应用的全部状态,将状态从组件中抽离出来,使得状态管理更加清晰易懂。在具有较多状态的应用中,必须使用模块

深入了解vuex的实现原理深入了解vuex的实现原理Mar 20, 2023 pm 06:14 PM

当面试被问vuex的实现原理,你要怎么回答?下面本篇文章就来带大家深入了解一下vuex的实现原理,希望对大家有所帮助!

在Vue应用中使用vuex时出现“Error: [vuex] unknown action type: xxx”怎么解决?在Vue应用中使用vuex时出现“Error: [vuex] unknown action type: xxx”怎么解决?Jun 25, 2023 pm 12:09 PM

在Vue.js项目中,vuex是一个非常有用的状态管理工具。它可以帮助我们在多个组件之间共享状态,并提供了一种可靠的方式来管理状态的变化。但在使用vuex时,有时会遇到“Error:[vuex]unknownactiontype:xxx”的错误。这篇文章将介绍该错误的原因及解决方法。1.错误原因在使用vuex时,我们需要定义一些actions和mu

在Vue应用中使用vuex时出现“TypeError: Cannot read property 'xxx' of undefined”怎么解决?在Vue应用中使用vuex时出现“TypeError: Cannot read property 'xxx' of undefined”怎么解决?Aug 18, 2023 pm 09:24 PM

在Vue应用中使用Vuex是非常常见的操作。然而,偶尔在使用Vuex时会遇到错误信息“TypeError:Cannotreadproperty'xxx'ofundefined”,这个错误信息的意思是无法读取undefined的属性“xxx”,导致了程序的错误。这个问题其实产生的原因很明显,就是因为在调用Vuex的某个属性的时候,这个属性没有被正确

vue3+vite中如何使用vuexvue3+vite中如何使用vuexJun 03, 2023 am 09:10 AM

具体步骤:1、安装vuex(vue3建议4.0+)pnpmivuex-S2、main.js中配置importstorefrom&#39;@/store&#39;//hx-app的全局配置constapp=createApp(App)app.use(store)3、新建相关的文件夹与文件,这里配置多个不同vuex内部的js,使用vuex的modules来放不同的页面,文件,然后统一使用一个getters.jsindex.js核心文件,这里使用了import.meta.glob,而不

在Vue应用中使用vuex时出现“Error: "xxx" has already been declared as a data property.”怎么解决?在Vue应用中使用vuex时出现“Error: "xxx" has already been declared as a data property.”怎么解决?Jun 24, 2023 pm 08:46 PM

在Vue应用的开发过程中,使用vuex来管理应用状态是非常常见的做法。然而,在使用vuex的过程中,有时我们可能会遇到这样的错误提示:“Error:'xxx'hasalreadybeendeclaredasadataproperty.”这个错误提示看起来很莫名其妙,但其实是由于在Vue组件中,使用了重复的变量名来定义data属性和vuex

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)