VUEX的基础

本文介绍了Vuex的基本概念,包括其作为状态管理模式的作用、如何通过mutations进行同步更新、actions处理异步操作,以及getters派生状态和Module模块化管理。通过实例演示了在项目中配置和使用Vuex的步骤。
摘要由CSDN通过智能技术生成

一、vuex是基本概念

  • 概念:是一个专为 Vue.js 应用程序开发的状态管理模式 ;
  • 作用:vuex是采用集中式管理组件依赖的共享数据的一个工具,可以解决不同组件数据共享问题;
  • 注意:
  1. 修改state状态必须通过mutations

  2. mutations只能执行同步代码,类似ajax,定时器之类的代码不能在mutations中执行

  3. 执行异步代码,要通过actions,然后将数据提交给mutations才可以完成

  4. state的状态即共享数据可以在组件中引用

  5. 组件中可以调用action

二、vuex基础-初始化功能

  1. 创建一个新的脚手架项目:vue create demo
  2. 开始初化,安装vuex,这里我们暂时使用3.6.2版本的:yarn add vuex@3.6.2
  3. 在main.js中引入:import Vuex from 'vuex'
  4. 在main.js中调用:Vue.use(vuex)
  5. 在main.js中设置配置项:const store = new Vuex.Store({...配置项})
  6. 在main.js中根实例配置 store 选项指向 store 实例对象:new Vue({....,store,...})

三、vuex的核心概念——state

// state是放置所有公共状态的属性,如果你有一个公共状态数据 , 你只需要定义在 state对象中


// 定义state
//初始化vuex对象
const store = new Vuex.Store({
  state: {
    // 管理数据
    count: 0
  }
})

//在组件中获取count有三种方式:
// 第一种:原始形式 插值表达式
<div> state的数据:{{ $store.state.count }}</div>

//第二种:计算属性 将state属性定义在计算属性中
  computed: {
    count () {
      return this.$store.state.count
    }
  }

<div> state的数据:{{ count }}</div>

// 第三种:辅助函数 mapState
import { mapState } from 'vuex'
computed: {
    ...mapState(['count'])
  }

<div> state的数据:{{ count }}</div>

四、vuex的核心概念——mutations

// state数据的修改只能通过mutations,并且mutations必须是同步更新,目的是形成数据快照
// 定义mutations
const store  = new Vuex.Store({
  state: {
    count: 0
  },
  // 定义mutations
  mutations: {
    // 方法里参数 第一个参数是当前store的state属性
    // payload 载荷 运输参数 调用mutaiions的时候 可以传递参数 传递载荷
    addCount (state) {
      state.count += 1
    }
  },
})

// 调用的第一种方法:原始形式 $store
<template>
  <button @click="addCount">+1</button>
</template>

<script>
export default {
    methods: {
    //   调用方法
      addCount () {
         // 调用store中的mutations 提交给muations
        // commit('muations名称', 2)
        this.$store.commit('addCount', 10)  // 直接调用mutations
    }
  }
}
</script>

addCount (state, payload) {
        state.count += payload
    }
    this.$store.commit('addCount', 10)

 
// 调用的第一种方法:辅助函数 mapMutations
import  { mapMutations } from 'vuex'
methods: {
    ...mapMutations(['addCount'])
}

<button @click="addCount(100)">+100</button>

五、vuex的核心概念——actions

// state是存放数据的,mutations是同步更新数据,actions则负责进行异步操作
// 定义actions
 actions: {
  //  获取异步的数据 context表示当前的store的实例 可以通过 context.state 获取状态 也可以通过context.commit 来提交mutations, 也可以 context.diapatch调用其他的action
    getAsyncCount (context) {
      setTimeout(function(){
        // 一秒钟之后 要给一个数 去修改state
        context.commit('addCount', 123)
      }, 1000)
    }
 }


// 第一种调用的方法:原始调用 $store
 addAsyncCount () {
     this.$store.dispatch('getAsyncCount')
 }

// 第二种调用的方法:传参调用
 addAsyncCount () {
     this.$store.dispatch('getAsyncCount', 123)
 }

// 第三种调用的方法:辅助函数  mapActions
import { mapActions } from 'vuex'
methods: {
    ...mapActions(['getAsyncCount'])
}

<button @click="getAsyncCount(111)">+异步</button>

六、vuex的核心概念——getters

// 除了state之外,有时我们还需要从state中派生出一些状态,这些状态是依赖state的,此时会用到getters
// 定义getters
getters: {
    // getters函数的第一个参数是 state
    // 必须要有返回值
     filterList:  state =>  state.list.filter(item => item > 5)
  }

// 使用getters的第一种方法:原始方式 $store
<div>{{ $store.getters.filterList }}</div>

// 使用getters的第二种方法:辅助函数  mapGetters
import { mapGetters} from 'vuex'
computed: {
    ...mapGetters(['filterList'])
}
<div>{{ filterList }}</div>

七、vuex的核心概念——Module

  • 由于使用单一状态树,应用的所有状态会集中到一个比较大的对象。当应用变得非常复杂时,store 对象就有可能变得相当臃肿,所以就需要用到模块化,也就是Module;
// 模块化的简单应用

// 定义两个模块   user 和  setting

// user中管理用户的状态  token 

// setting中管理 应用的名称 name
const store  = new Vuex.Store({
  modules: {
    user: {
       state: {
         token: '12345'
       }
    },
    setting: {
      state: {
         name: 'Vuex实例'
      }
    }
  })

// 定义child-b组件,分别显示用户的token和应用名称name
<template>
  <div>
      <div>用户token {{ $store.state.user.token }}</div>
      <div>网站名称 {{ $store.state.setting.name }}</div>
  </div>
</template>

// 请注意: 此时要获取子模块的状态 需要通过 $store.**`state`**.**`模块名称`**.**`属性名`** 来获取
 getters: {
   token: state => state.user.token,
   name: state => state.setting.name
 } 

// 请注意:这个getters是根级别的getters哦

// 通过mapGetters引用
 computed: {
       ...mapGetters(['token', 'name'])
 }
  • 模块化中的命名空间:想保证内部模块的高封闭性,我们可以采用namespaced来进行设置
  user: {
       namespaced: true,
       state: {
         ....
       },
       mutations: {
        ....
    }
  },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值