likes
comments
collection
share

潦草介绍下Vuex的state、mutations

作者站长头像
站长
· 阅读数 16

····为什么会有Vuex ?

潦草介绍下Vuex的state、mutations

在现代 Web 开发复杂多变的需求驱动之下,组件化开发已然成为了事实上的标准。

然而大多数场景下的组件都并不是独立存在的,而是相互协作共同构成了一个复杂的业务功能。

···组件间的通信成为了必不可少的开发需求

潦草介绍下Vuex的state、mutations

潦草介绍下Vuex的state、mutations

····Vuex 是什么

潦草介绍下Vuex的state、mutations

Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式。它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化。

Vuex是采用集中式管理组件依赖的共享数据的一个工具, 可以解决不同组件数据共享的问题

潦草介绍下Vuex的state、mutations

潦草介绍下Vuex的state、mutations

结论

  1. 修改state状态必须通过mutations
  2. mutations只能执行同步代码,类似ajax,定时器之类的代码不能在mutations中执行
  3. 执行异步代码,要通过actions,然后将数据提交给mutations才可以完成
  4. state的状态即共享数据可以在组件中引用
  5. 组件中可以调用action

vuex 初始化功能

潦草介绍下Vuex的state、mutations

····安装

第一步:

下载包

npm i vuex@版本号 

第二步:

引入和挂载

h(App)","marks":[]}]}]},{"type":"block","id":"JXhK-1654345740388","name":"code-line","data":{},"nodes":[{"type":"text","id":"aiVp-1654345740387","leaves":[{"text":"}).$mount('#app')","marks":[]}]}]},{"type":"block","id":"WaC4-1654345740390","name":"code-line","data":{},"nodes":[{"type":"text","id":"yBQw-1654345740389","leaves":[{"text":"","marks":[]}]}]},{"type":"block","id":"ZwFS-1654345714527","name":"code-line","data":{},"nodes":[{"type":"text","id":"OVJT-1654344977321","leaves":[{"text":"","marks":[]}]}]}],"state":{}}]'>

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

state

潦草介绍下Vuex的state、mutations

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

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

如何在组件中获取count?

原始形式- 插值表达式 App.vue

组件中可以使用 this.$store 获取到vuex中的store对象实例,可通过state属性属性获取count, 如下

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

计算属性 - 将state属性定义在计算属性中

目的是使用插值表达式会导致代码量过多,在此用计算属性进行优化

// 把state中数据,定义在组件内的计算属性中 computed: { count () { return this.$store.state.count } }
<div> state的数据:{{ count }}</div>

辅助函数 - mapState

mapState是辅助函数,帮助我们把store中的数据映射到 组件的计算属性中, 它属于一种方便用法

写在计算属性中

用法 : 第一步:导入mapState

import { mapState } from 'vuex'

第二步:采用数组形式引入state属性,加上延展计算符

...mapState(['count'])

第三步:用插值表达式,直接写入count

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

mutations

潦草介绍下Vuex的state、mutations

写入事件对象methods中

state数据的修改只能通过mutations,并且mutations必须是同步更新,目的是形成数据快照

数据快照:一次mutation的执行,立刻得到一种视图状态,因为是立刻,所以必须是同步 定义mutations

const store = new Vuex.Store({ state: { count: 0 }, // 定义mutations,与state同级 mutations: { } })

格式说明

mutations是一个对象,对象中存放修改state的方法

mutations: { // 方法里参数 第一个参数是当前store的state属性 // payload 载荷 运输参数 调用mutaiions的时候 可以传递参数 传递载荷 addCount (state,payload) { state.count += payload } },

如何在组件中调用mutations

原始形式-$store

","marks":[]}]}]}],"state":{}}]'>

<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

mapMutations和mapState很像,它把位于mutations中的方法提取了出来,我们可以将它导入

import { mapMutations } from 'vuex' methods: { ...mapMutations(['addCount']) }

上面代码的含义是将mutations的方法导入了methods中,等同于

methods: { // commit(方法名, 载荷参数) addCount () { this.$store.commit('addCount') } }

此时,就可以直接通过this.addCount调用了

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

但是请注意: Vuex中mutations中要求不能写异步代码,如果有异步的ajax请求,应该放置在actions中

转载自:https://juejin.cn/post/7106109507305996319
评论
请登录