亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vuex中的5個(gè)屬性使用方法舉例講解

 更新時(shí)間:2023年04月27日 09:27:16   作者:hhzz  
vuex是專(zhuān)門(mén)為Vue.js應(yīng)用程序開(kāi)發(fā)的狀態(tài)管理模式,下面這篇文章主要給大家介紹了關(guān)于vuex中5個(gè)屬性使用方法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下

一,Vuex簡(jiǎn)介

Vuex是Vue.js的狀態(tài)管理庫(kù),它通過(guò)中心化的狀態(tài)管理使得組件間的數(shù)據(jù)共享更加容易。

Vuex包含五個(gè)核心屬性:state、getters、mutations、actions和modules。

Vuex是Vue.js的狀態(tài)管理庫(kù),它提供了一種集中式存儲(chǔ)管理應(yīng)用程序中所有組件的狀態(tài),并將其分離到一個(gè)可預(yù)測(cè)的狀態(tài)容器中。Vuex包括五個(gè)核心屬性:

二,Vuex五個(gè)核心屬性

1:state

state:定義了應(yīng)用程序的狀態(tài),就是我們要管理的數(shù)據(jù)。

存放應(yīng)用程序的狀態(tài)(數(shù)據(jù)),所有組件共享。它是Vue實(shí)例的data屬性的替代品,但是通過(guò)它存儲(chǔ)和管理的狀態(tài),可以在整個(gè)應(yīng)用程序中實(shí)現(xiàn)全局共享,即不同的組件可以通過(guò)定義的getter和setter訪問(wèn)同一狀態(tài)數(shù)據(jù)。

const store = new Vuex.Store({
  state: {
    count: 0
  }
})

2:getters

getters:用于獲取State中的狀態(tài),主要用于對(duì)state進(jìn)行邏輯上的組合和應(yīng)用,類(lèi)似于Vue組件中的計(jì)算屬性。

const store = new Vuex.Store({
  state: {
    count: 0
  },
  getters: {
    doubleCount(state) {
      return state.count * 2
    }
  }
})

3:mutations

mutations:用于修改state中的數(shù)據(jù),是唯一可以修改state的地方。mutations接收state作為第一個(gè)參數(shù),接收payload作為第二個(gè)參數(shù)。
用于修改State中的狀態(tài),只能同步執(zhí)行。Mutation必須是同步函數(shù),因?yàn)樗鼈儾荒芴幚懋惒叫袨椋惒叫袨閼?yīng)該放在Action中處理。

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment(state) {
      state.count++
    },
    add(state, payload) {
      state.count += payload
    }
  }
})

4:actions

actions:用于異步操作和提交mutations,在actions中可以進(jìn)行任何異步操作,最后再提交到mutations中同步修改state。actions接收context作為第一個(gè)參數(shù),其中包含了state、getters和commit等屬性。

可以包含任意異步操作(例如從服務(wù)器獲取數(shù)據(jù)),可以用Mutation通過(guò)提交(commit)來(lái)修改State。

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment(state) {
      state.count++
    }
  },
  actions: {
    incrementAsync(context) {
      setTimeout(() => {
        context.commit('increment')
      }, 1000)
    }
  }
})

5:modules

modules:用于將store分割成模塊,每個(gè)模塊都擁有自己的state、mutation、action、getters和子模塊,以便提高應(yīng)用程序的可維護(hù)性。

將State、Getter、Mutation、Action模塊化,便于組件化和模塊化開(kāi)發(fā)。

const store = new Vuex.Store({
  modules: {
    cart: {
      state: {
        items: []
      },
      mutations: {
        addItem(state, item) {
          state.items.push(item)
        }
      },
      actions: {
        addAsyncItem(context, item) {
          setTimeout(() => {
            context.commit('addItem', item)
          }, 1000)
        }
      }
    }
  }
})

使用方法示例:

const store = new Vuex.Store({
  modules: {
    cart: {
      state: {
        items: []
      },
      mutations: {
        pushProductToCart (state, payload) {
          state.items.push({
            id: payload.id,
            quantity: 1
          })
        }
      },
      actions: {
        addProductToCart ({ state, commit }, product) {
          const cartItem = state.items.find(item => item.id === product.id)
          if (!cartItem) {
            commit('pushProductToCart', product)
          }
        }
      },
      getters: {
        cartItems: state => {
          return state.items
        }
      }
    }
  }
})
這個(gè)代碼創(chuàng)建了一個(gè)包含cart模塊的Vuex store對(duì)象,其中cart模塊包含state、mutations、actions和getters四個(gè)屬性,用于管理購(gòu)物車(chē)數(shù)據(jù)。在addProductToCart action中,使用state.items和commit方法來(lái)修改cart模塊中的數(shù)據(jù)。在cartItems getter中,使用state.items來(lái)計(jì)算購(gòu)物車(chē)中的商品數(shù)量和總價(jià)。

三,Vuex使用方法

使用方法:

1:安裝Vuex:npm install vuex --save

2:在main.js中,導(dǎo)入Vuex,并使用Vue.use()方法注冊(cè)Vuex。

import Vue from 'vue'
import Vuex from 'vuex'
import App from './App.vue'
Vue.use(Vuex)
const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment(state) {
      state.count++
    }
  }
})
new Vue({
  store,
  render: h => h(App)
}).$mount('#app')

3:在組件中使用vuex中的數(shù)據(jù)和方法。

<template>
  <div>
    <p>Count: {{ count }}</p>
    <button @click="increment">Increment</button>
  </div>
</template>
<script>
export default {
  computed: {
    count() {
      return this.$store.state.count
    }
  },
  methods: {
    increment() {
      this.$store.commit('increment')
    }
  }
}
</script>

4:vuex綜合案例

下面是一個(gè)簡(jiǎn)單的Vuex使用方法的示例:

// 引入Vue和Vuex
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
// 創(chuàng)建一個(gè)Store
const store = new Vuex.Store({
  // 定義State
  state: {
    count: 0
  },
  // 定義Mutation
  mutations: {
    increment: state => state.count++,
    decrement: state => state.count--
  },
  // 定義Getter
  getters: {
    evenOrOdd: state => state.count % 2 === 0 ? 'even' : 'odd'
  },
  // 定義Action
  actions: {
    incrementIfOdd ({ commit, state }) {
      if ((state.count + 1) % 2 === 0) {
        commit('increment')
      }
    }
  }
})
new Vue({
  el: '#app',
  store,
  template: `
    <div>
      <p>Count: {{ count }}</p>
      <p>Even or Odd? {{ evenOrOdd }}</p>
      <button @click="increment">Increment</button>
      <button @click="decrement">Decrement</button>
      <button @click="incrementIfOdd">IncrementIfOdd</button>
    </div>
  `,
  computed: {
    count () {
      return this.$store.state.count
    },
    evenOrOdd () {
      return this.$store.getters.evenOrOdd
    }
  },
  methods: {
    increment () {
      this.$store.commit('increment')
    },
    decrement () {
      this.$store.commit('decrement')
    },
    incrementIfOdd () {
      this.$store.dispatch('incrementIfOdd')
    }
  }
})

這個(gè)代碼創(chuàng)建了一個(gè)Vuex Store,并定義了State、Mutation、Getter、Action。然后將Store實(shí)例與Vue實(shí)例關(guān)聯(lián)。在Vue組件中,使用計(jì)算屬性(computed)和方法(methods)來(lái)訪問(wèn)State、Getter和Action。在方法中,使用commit來(lái)提交Mutation,使用dispatch來(lái)分發(fā)Action。

總結(jié)

到此這篇關(guān)于vuex中的5個(gè)屬性使用方法的文章就介紹到這了,更多相關(guān)vuex的5個(gè)屬性使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論