vuex概念
1.1、组件之间共享数据的方式
父 向 子 传值: v-bind属性绑值
子 向 父 传值:v-on 事件绑定
兄弟组件之间共享数据: EventBus
$on 接收数据的那个组件 $em IT 发送数据的那个组件
1.2、Vuex 是什么
Vuex是实现组件全局状态(数据)管理的一种机制,可以方便的实现组件之间数据的共享。
1.3、使用Vuex同意管理状态的好处
能够在vuex中集中管理共享的数据,易于开发和后期维护
能够高效地实现组件之间的数据共享, 提高 开发效率
存储在vuex中的数据都是响应式的,能够实时保持数据与页面的同步。
1.4、什么样的数据适合存储到Vuex中
一般情况下,只有组件之间共享的数据,才有必要存储到vuex中;对于组件中的私有数据,依然存储在组件自身的data中即可。
vuex的基本使用
1、安装vuex依赖包
npm i vuex - -s ave
2、导入vuex包
import Vuex From 'vuex' Vue.use (Vuex)
3、创建stroe对象
const Store = new Vuex.Store({ // stat e中 存放 的就是全局共享的数据 state:{count:0} })
4、将store对象挂载到 vue实例 中
new Vue({ el:' # app', render:h => h(app), router, //将创建的共享数据对象,挂载到vue实例中 //所有的组件,就可以直接从stroe中获取全局的数据了 store })
vuex的核心概念
1、vuex中的主要核心概念如下
State
State提供唯一的公共数据 源 , 所有共享的数据都要统放到Store的State中进行存储。
export default new Vuex.Store({ state: { count: 0 }, })
组件访问State中数据的**第一种方式:**Addition.vue
this.$store.state.全局数据名称 <h3>当前最新的count值为:{{$store.state.count}}</h3>
组件访问State中数据的 第二种方式: Su BT raction.vue
// 1. 从 vuex 中按需导入 mapState 函数 import { mapState } f rom 'vuex'
通过刚才导入的 mapState 函数,将当前组件需要的全局数据,映射为当前组件的 computed 计算属性:
<h3>当前最新的count值为:{{count}}</h3> // 2. 将全局数据,映射为当前组件的计算属性 computed: { .. .mapState(['count']) }
Mutation
Mutation 用于变更 Store中 的数据。
① 只能通过 mutation 变更 Store 数据 ,不可以直接操作 Store 中的数据。
② 通过这种方式虽然操作起来稍微繁琐一些,但是可以集中监控所有数据的变化。
第一种方式
(1)
// 定义 Mutation store.js const store = new Vuex.Store({ state: { count: 0 }, mutations: { add(state) { // 不要在 mutations 函数中,执行 异步操作 // setTimeout(() => { // state.count++ // }, 1000) // 变更状态 state.count++ } } })
在组件中访问Mutation Addition.vue
<button @click="btnHandler1">+1</button> methods:{ //触发mutation btnHandler1() { //触发 mutations 的第一种方式 this.$store .COM mit('add') }, }
(2)可以在触发 mutations 时传递参数:
// 定义Mutation const store = new Vuex.Store({ state: { count: 0 }, mutations: { addN(state, step) { // 变更状态 state.count += step } } })
在组件中访问Mutation Addition.vue
<button @click="btnHandler2">+N</button> methods: { btnHandler2() { // commit 的作用,就是调用 某个 mutation 函数 携带参数 this.$store.commit('addN', 3) }, }
第二种方式
this.$store.commit() 是触发 mutations 的第一种方式,触发 mutations 的第二种方式:
// 1. 从 vuex 中按需导入 mapMutations 函数 import { mapMutations } from 'vuex'
通过刚才导入的 mapMutations 函数,将需要的 mutations 函数,映射为当前组件的 methods 方法:
// 2. 将指定的 mutations 函数,映射为当前组件的 methods 函数 methods: { ...mapMutations(['add', 'addN']) }
完整代码:
//store.js e xp ort default new Vuex.Store({ state: { count: 0 }, // 只有 mutations 中定义的函数,才有权利修改 state 中的数据 mutations: { //减减 s ub (state) { state.count-- }, //携带参数 subN(state, step) { state.count -= step } }, })
在组件中Subtraction.vue
<h3>当前最新的count值为:{{count}}</h3> <button @click="btnHandler1">-1</button> //<button @click="btnHandler2">-1</button> <button @click="subN(3)">-N</button> -----携带参数 import { mapState, mapMutations} from 'vuex' computed: { ...mapState(['count']), }, methods: { ...mapMutations(['sub', 'subN']), btnHandler1() { this.sub() }, //btnHandler2() { //this.subN(3) //}, }
Action
(1)Action 用于 处理异步 任务 。
如果通过异步操作变更数据,**必须通过 Action,而不能使用 Mutation,**但是在 Action 中还是要通过触发Mutation 的方式间接变更数据。
第一种方式(1)处理异步任务
// 定义 Action const store = new Vuex.Store({ // ...省略其他代码 mutations: { add(state) { state.count++ } }, actions: { addAsync(context) { setTimeout(() => { context.commit('add') }, 1000) } } })
在组件中Addition.vue
<button @click="btnHandler3">+1 Async</button> // 异步地让 count 自增 +1 btnHandler3() { // 这里的 dispatch 函数,专门用来触发 action this.$store.dispatch('addAsync') },
(2)携带参数
触发 actions 异步任务时携带参数:
// 定义 Action const store = new Vuex.Store({ // ...省略其他代码 mutations: { addN(state, step) { -------------携带参数 state.count += step } }, actions: { addNAsync(context, step) { -------------携带参数 setTimeout(() => { context.commit('addN', step) }, 1000) } } })
在组件中
<button @click="btnHandler4">+N Async</button> btnHandler4() { this.$store.dispatch('addNAsync', 5) }第二种方式
this.$store.dispatch() 是触发 actions 的第一种方式,触发 actions 的第二种方式:
// 1. 从 vuex 中按需导入 mapActions 函数 import { mapActions } from 'vuex'
通过刚才导入的 mapActions 函数,将需要的 actions 函数,映射为当前组件的 methods 方法:
// 2. 将指定的 actions 函数,映射为当前组件的 methods 函数 methods: { ...mapActions(['addASync', 'addNASync']) }
完整代码
export default new Vuex.Store({ state: { count: 0 }, // 只有 mutations 中定义的函数,才有权利修改 state 中的数据 mutations: { //减减 sub(state) { state.count-- }, subN(state, step) { state.count -= step } }, //actions可以处理异步任务 actions: { subAsync(context) { setTimeout(() => { // 在 actions 中,不能直接修改 state 中的数据; // 必须通过 context.commit() 触发某个 mutation 才行 context.commit('sub') }, 1000) }, subNAsync(context, step) { setTimeout(() => { context.commit('subN', step) }, 1000) } }, })
组件Subtraction.vue
<h3>当前最新的count值为:{{count}} <button @click="subAsync">-1 Async</button> <button @click="subNAsync(5)">-N Async</button> import { mapState, mapMutations, mapActions } from 'vuex' methods: { ...mapActions(['subAsync', 'subNAsync']), }
**Getter **
不会修改state里面的数据
Getter 用于对 Store 中的数据进行加工处理形成新的数据。
① Getter 可以对 Store 中已有的数据加工处理之后形成新的数据,类似 Vue 的计算属性。
② Store 中数据发生变化,Getter 的数据也会跟着变化。
// 定义 Getter const store = new Vuex.Store({ state: { count: 0 }, getters: { showNum: state => { return '当前最新的数量是【'+ state.count +'】' } } })
使用getters的第一种方式
this.$store.getters.名称
使用getters的第一种方式
{{showNum}} import { mapGetters } from 'vuex' computed: { ...mapGetters(['showNum']) }只有 mutations 中定义的函数,才有权利 修改 state 中的数据 在 actions 中, 不能直接修改 state 中的数据 ; 必须通过 context.commit() 触发某个 mutation 才行 commit 的作用,就是调用 某个 mutation 函数 dispatch 函数,专门用来触发 action
到此这篇关于vuex的几个属性及其使用传参的 文章 就介绍到这了,更多相关vuex属性使用传参内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
您可能感兴趣的文章: Vuex给state中的对象新添加属性遇到的问题及解决 几分钟弄懂Vuex的五大属性和使用方式 详解Vuex的属性 vuex中的state属性解析 详解Vuex的属性和作用 说说Vuex的getters属性的具体用法 Vuex的actions属性的具体使用 Vue唯一可以更改vuex实例中state数据状态的属性对象Mutation的讲解
总结
以上是 为你收集整理的 vuex的几个属性及其使用传参方式 全部内容,希望文章能够帮你解决 vuex的几个属性及其使用传参方式 所遇到的问题。
如果觉得 网站内容还不错, 推荐好友。
查看更多关于vuex的几个属性及其使用传参方式的详细内容...