Vue - vuex

Vue Vuex 是一种状态管理模式,用于解决多个组件间共享状态的问题。核心概念包括响应式的state、getters、mutations和actions。State用于存储共享数据,通过计算属性、mapState辅助函数等方式在组件中获取。Getters允许派生状态,避免重复代码。Mutations是同步更改状态的唯一方法,通常配合载荷使用。Actions则允许执行异步操作并提交mutations。通过这些机制,Vuex使状态管理变得高效且易于维护。
摘要由CSDN通过智能技术生成

Vuex状态管理模式

当两个或两个以上的组件需要共用同一个数据源时,使用Vuex对数据的管理会变得更见简单轻松(相比localStorage、路由传参)

响应式

Vuex的状态存储是响应式的。当Vue组件从store中读取状态的时候,若store中的状态发生变化,那么相应的组件(视图)也会相应地得到高效更新。

核心概念

  • state
  • getters
  • mutations
  • actions

state

存放共享的数据,需要提前声明

// store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 0
  }
})

// main.js,注入store,每个组件都可以通过this.$store访问store
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store/store'

new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})

如何在组件中获取state中的数据?

  • 直接调用
let count = this.$store.state.count
this.count = count

<span>{
  {count}}</span>
  • 计算属性
// 由于Vuex的状态存储是响应式的,从store实例中读取状态最简单的方法就是在计算属性中返回某个状态
// 每当 store.state.count 变化的时候, 都会重新求取计算属性,并且触发更新相关联的 DOM
computed: {
  count () {
    return this.$store.state.count
  }
}

<span>{
  {count}}</span>
  • mapState辅助函数
// 当一个组件需要获取多个状态时候,将这些状态都声明为计算属性会有些重复和冗余。
// 为了解决这个问题,我们可以使用 mapState 辅助函数帮助我们生成计算属性,让你少按几次键:
// 不用写那么多次this.$store.state.xxx
import { mapState } from 'vuex'

export default {
  // mapState函数传入一个对象
  computed: mapState({
    // 箭头函数可使代码更简练
    count: state => state.count,
    
    // 传字符串参数 'count
Vue.js是一个流行的JavaScript框架,它允许您构建动态Web应用程序。Vuex是一个专为Vue.js应用程序开发的状态管理模式。它允许您在应用程序管理和维护状态,例如用户信息、购物车、主题等。Vuex将状态存储在一个集的存储库,称为store。Vuex的核心概念包括state、mutations、actions和getters。 - state:存储应用程序级别的状态,可以通过store.state访问。 - mutations:用于更改状态的函数,必须是同步函数。可以通过store.commit方法调用。 - actions:用于处理异步操作的函数,可以包含任意异步操作。可以通过store.dispatch方法调用。 - getters:用于从store获取状态的函数,可以通过store.getters访问。 下面是一个简单的示例,演示如何在Vue.js应用程序使用Vuex: 1.安装Vuex ```shell npm install vuex --save ``` 2.创建store ```javascript import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment (state) { state.count++ } }, actions: { incrementAsync ({ commit }) { setTimeout(() => { commit('increment') }, 1000) } }, getters: { getCount: state => { return state.count } } }) export default store ``` 3.在Vue组件使用store ```javascript <template> <div> <p>Count: {{ count }}</p> <button @click="increment">Increment</button> <button @click="incrementAsync">Increment Async</button> </div> </template> <script> import { mapGetters, mapActions } from 'vuex' export default { computed: { ...mapGetters([ 'getCount' ]) }, methods: { ...mapActions([ 'increment', 'incrementAsync' ]) } } </script> ``` 在上面的示例,我们创建了一个名为count的状态,并定义了一个名为increment的mutation和一个名为incrementAsync的action。我们还定义了一个名为getCount的getter,用于从store获取count状态。在Vue组件,我们使用mapGetters和mapActions帮助程序将getter和action映射到组件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值