vuex概念
1.1、组件之间共享数据的方式
父 向 子 传值:v-bind属性绑值
子 向 父 传值:v-on事件绑定
兄弟组件之间共享数据:eventbus
- $on 接收数据的那个组件
- $emit 发送数据的那个组件
1.2、vuex是什么
vuex是实现组件全局状态(数据)管理的一种机制,可以方便的实现组件之间数据的共享。
1.3、使用vuex同意管理状态的好处
能够在vuex中集中管理共享的数据,易于开发和后期维护
能够高效地实现组件之间的数据共享,提高开发效率
存储在vuex中的数据都是响应式的,能够实时保持数据与页面的同步。
1.4、什么样的数据适合存储到vuex中
一般情况下,只有组件之间共享的数据,才有必要存储到vuex中;对于组件中的私有数据,依然存储在组件自身的data中即可。
vuex的基本使用
1、安装vuex依赖包
npm i vuex --save
2、导入vuex包
import vuex from 'vuex' vue.use(vuex)
3、创建stroe对象
const store = new vuex.store({ //state中存放的就是全局共享的数据 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中数据的第二种方式: subtraction.vue
// 1. 从 vuex 中按需导入 mapstate 函数 import { mapstate } from '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.commit('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 export default new vuex.store({ state: { count: 0 }, // 只有 mutations 中定义的函数,才有权利修改 state 中的数据 mutations: { //减减 sub(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属性使用传参内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论