vuex中的属性怎么使用

前端开发   发布日期:前天 13:25   浏览次数:160

本文小编为大家详细介绍“vuex中的属性怎么使用”,内容详细,步骤清晰,细节处理妥当,希望这篇“vuex中的属性怎么使用”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

    一,Vuex简介

    Vuex是Vue.js的状态管理库,它通过中心化的状态管理使得组件间的数据共享更加容易。

    Vuex包含五个核心属性:state、getters、mutations、actions和modules。

    Vuex是Vue.js的状态管理库,它提供了一种集中式存储管理应用程序中所有组件的状态,并将其分离到一个可预测的状态容器中。Vuex包括五个核心属性:

    二,Vuex五个核心属性

    1:state

    state:定义了应用程序的状态,就是我们要管理的数据。

    存放应用程序的状态(数据),所有组件共享。它是Vue实例的data属性的替代品,但是通过它存储和管理的状态,可以在整个应用程序中实现全局共享,即不同的组件可以通过定义的getter和setter访问同一状态数据。

    1. const store = new Vuex.Store({
    2. state: {
    3. count: 0
    4. }
    5. })

    2:getters

    getters:用于获取State中的状态,主要用于对state进行逻辑上的组合和应用,类似于Vue组件中的计算属性。

    1. const store = new Vuex.Store({
    2. state: {
    3. count: 0
    4. },
    5. getters: {
    6. doubleCount(state) {
    7. return state.count * 2
    8. }
    9. }
    10. })

    3:mutations

    mutations:用于修改state中的数据,是唯一可以修改state的地方。mutations接收state作为第一个参数,接收payload作为第二个参数。
    用于修改State中的状态,只能同步执行。Mutation必须是同步函数,因为它们不能处理异步行为,异步行为应该放在Action中处理。

    1. const store = new Vuex.Store({
    2. state: {
    3. count: 0
    4. },
    5. mutations: {
    6. increment(state) {
    7. state.count++
    8. },
    9. add(state, payload) {
    10. state.count += payload
    11. }
    12. }
    13. })

    4:actions

    actions:用于异步操作和提交mutations,在actions中可以进行任何异步操作,最后再提交到mutations中同步修改state。actions接收context作为第一个参数,其中包含了state、getters和commit等属性。

    可以包含任意异步操作(例如从服务器获取数据),可以用Mutation通过提交(commit)来修改State。

    1. const store = new Vuex.Store({
    2. state: {
    3. count: 0
    4. },
    5. mutations: {
    6. increment(state) {
    7. state.count++
    8. }
    9. },
    10. actions: {
    11. incrementAsync(context) {
    12. setTimeout(() => {
    13. context.commit('increment')
    14. }, 1000)
    15. }
    16. }
    17. })

    5:modules

    modules:用于将store分割成模块,每个模块都拥有自己的state、mutation、action、getters和子模块,以便提高应用程序的可维护性。

    将State、Getter、Mutation、Action模块化,便于组件化和模块化开发。

    1. const store = new Vuex.Store({
    2. modules: {
    3. cart: {
    4. state: {
    5. items: []
    6. },
    7. mutations: {
    8. addItem(state, item) {
    9. state.items.push(item)
    10. }
    11. },
    12. actions: {
    13. addAsyncItem(context, item) {
    14. setTimeout(() => {
    15. context.commit('addItem', item)
    16. }, 1000)
    17. }
    18. }
    19. }
    20. }
    21. })

    使用方法示例:

    1. const store = new Vuex.Store({
    2. modules: {
    3. cart: {
    4. state: {
    5. items: []
    6. },
    7. mutations: {
    8. pushProductToCart (state, payload) {
    9. state.items.push({
    10. id: payload.id,
    11. quantity: 1
    12. })
    13. }
    14. },
    15. actions: {
    16. addProductToCart ({ state, commit }, product) {
    17. const cartItem = state.items.find(item => item.id === product.id)
    18. if (!cartItem) {
    19. commit('pushProductToCart', product)
    20. }
    21. }
    22. },
    23. getters: {
    24. cartItems: state => {
    25. return state.items
    26. }
    27. }
    28. }
    29. }
    30. })
    31. 这个代码创建了一个包含cart模块的Vuex store对象,其中cart模块包含statemutationsactionsgetters四个属性,用于管理购物车数据。在addProductToCart action中,使用state.itemscommit方法来修改cart模块中的数据。在cartItems getter中,使用state.items来计算购物车中的商品数量和总价。

    三,Vuex使用方法

    使用方法:

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

    2:在main.js中,导入Vuex,并使用Vue.use()方法注册Vuex。

    1. import Vue from 'vue'
    2. import Vuex from 'vuex'
    3. import App from './App.vue'
    4. Vue.use(Vuex)
    5. const store = new Vuex.Store({
    6. state: {
    7. count: 0
    8. },
    9. mutations: {
    10. increment(state) {
    11. state.count++
    12. }
    13. }
    14. })
    15. new Vue({
    16. store,
    17. render: h => h(App)
    18. }).$mount('#app')

    3:在组件中使用vuex中的数据和方法。

    1. <template>
    2. <div>
    3. <p>Count: {{ count }}</p>
    4. <button @click="increment">Increment</button>
    5. </div>
    6. </template>
    7. <script>
    8. export default {
    9. computed: {
    10. count() {
    11. return this.$store.state.count
    12. }
    13. },
    14. methods: {
    15. increment() {
    16. this.$store.commit('increment')
    17. }
    18. }
    19. }
    20. </script>

    4:vuex综合案例

    下面是一个简单的Vuex使用方法的示例:

    1. // 引入Vue和Vuex
    2. import Vue from 'vue'
    3. import Vuex from 'vuex'
    4. Vue.use(Vuex)
    5. // 创建一个Store
    6. const store = new Vuex.Store({
    7. // 定义State
    8. state: {
    9. count: 0
    10. },
    11. // 定义Mutation
    12. mutations: {
    13. increment: state => state.count++,
    14. decrement: state => state.count--
    15. },
    16. // 定义Getter
    17. getters: {
    18. evenOrOdd: state => state.count % 2 === 0 ? 'even' : 'odd'
    19. },
    20. // 定义Action
    21. actions: {
    22. incrementIfOdd ({ commit, state }) {
    23. if ((state.count + 1) % 2 === 0) {
    24. commit('increment')
    25. }
    26. }
    27. }
    28. })
    29. new Vue({
    30. el: '#app',
    31. store,
    32. template: `
    33. <div>
    34. <p>Count: {{ count }}</p>
    35. <p>Even or Odd? {{ evenOrOdd }}</p>
    36. <button @click="increment">Increment</button>
    37. <button @click="decrement">Decrement</button>
    38. <button @click="incrementIfOdd">IncrementIfOdd</button>
    39. </div>
    40. `,
    41. computed: {
    42. count () {
    43. return this.$store.state.count
    44. },
    45. evenOrOdd () {
    46. return this.$store.getters.evenOrOdd
    47. }
    48. },
    49. methods: {
    50. increment () {
    51. this.$store.commit('increment')
    52. },
    53. decrement () {
    54. this.$store.commit('decrement')
    55. },
    56. incrementIfOdd () {
    57. this.$store.dispatch('incrementIfOdd')
    58. }
    59. }
    60. })

    这个代码创建了一个Vuex Store,并定义了State、Mutation、Getter、Action。然后将Store实例与Vue实例关联。在Vue组件中,使用计算属性(computed)和方法(methods)来访问State、Getter和Action。在方法中,使用commit来提交Mutation,使用dispatch来分发Action。

    以上就是vuex中的属性怎么使用的详细内容,更多关于vuex中的属性怎么使用的资料请关注九品源码其它相关文章!