概述
Vuex(组件之间通讯) 是一个专为 Vue.js 应用程序开发的状态管理模式。一般使用与大型项目。
-
vuex是采用集中式管理组件依赖的共享数据的一个工具,可以解决不同组件数据共享问题。
总结:
-
state: 用来定义数据
-
修改state状态必须通过
mutations
-
mutations
只能执行同步代码,类似ajax,定时器之类的代码不能在mutations中执行 -
执行异步代码,要通过actions,然后将数据提交给mutations才可以完成
-
getters从 store 中的 state 中派生出一些状态
-
modules: 进入项目学习
vuex基础-初始化
安装:yarn add vuex@3.6.2
-
在main.js中
import Vuex from 'vuex'
-
在main.js中
Vue.use(Vuex)
=> 调用了 vuex中的 一个install方法 -
const store = new Vuex.Store({...配置项})
-
在根实例配置 store 选项指向 store 实例对象
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(vuex)
const store = new Vuex.Store({})
new Vue({
el: '#app',
store
})
vuex基础-state
state是放置所有公共状态的属性,如果你有一个公共状态数据 , 你只需要定义在 state对象中
定义state
const store = new Vuex.Store({
state: {
uname: 'ha',
count: 0,
word: '',
},
原始形式- 插值表达式
组件中可以使用 this.$store 获取到vuex中的store对象实例,可通过state属性属性获取count, 如下
<div> state的数据:{{ $store.state.count }}</div>
计算属性 - 将state属性定义在计算属性中
computed: {
count () {
return this.$store.state.count
}
}
辅助函数 - mapState
mapState是辅助函数,帮助我们把store中的数据映射到 组件的计算属性中, 它属于一种方便用法
属于谁的方法: vuex
是什么: 是一个函数
作用: 将vuex中state的数据映射为了计算属性
怎么用: 调用,传递数组,数组每一项,就是state中的数据名,写哪个数据,就会将哪个数据处理为计算属性
返回值: 对象, 存的就是处理好的计算属性
第一步:导入mapState
import { mapState } from 'vuex'
第二步:采用数组形式引入state属性
mapState(['count'])
count () {
return this.$store.state.count
}
第三步:利用延展运算符将导出的状态映射给计算属性
computed: {
...mapState(['count'])
}<div> state的数据:{{ count }}</div>
vuex基础-mutations
state数据的修改只能通过mutations,并且mutations必须是同步更新,目的是形成
数据快照
数据快照:一次mutation的执行,立刻得到一种视图状态,因为是立刻,所以必须是同步
定义mutations
mutations是一个对象,对象中存放修改state的方法
mutations: {
// 方法里参数 第一个参数是当前store的state属性
// payload 载荷 运输参数 调用mutaiions的时候 可以传递参数 传递载荷
addCount (state,payload) {
state.count += 1
}
},
<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'])
}
注意: Vuex中mutations中要求不能写异步代码,如果有异步的ajax请求,应该放置在actions中
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'])
}
直接通过 this.方法就可以调用
<button @click="getAsyncCount(111)">+异步</button>
vuex基础-getters
除了state之外,有时我们还需要从state中派生出一些状态,这些状态是依赖state的,此时会用到getters
定义getters
getters: {
// getters函数的第一个参数是 state
// 必须要有返回值
filterList: state => state.list.filter(item => item > 5)
}<div>{{ $store.getters.filterList }}</div>
辅助函数 - mapGetters
computed: {
...mapGetters(['filterList'])
}<div>{{ filterList }}</div>
综合案例:
<template>
<div>
<!-- {{ $store.state.uname }}
{{ $store.state.count }} -->
<h1>{{ count }}</h1>
<br />
<h2>{{ uname }}</h2>
<br />
<button @click="addCount">add</button>
<button @click="delCount">sub</button>
<button @click="asyncadd">异步增加</button>
<h1>getters里的平方{{ $store.getters.countSqu }}</h1>
<h5>mapGetters平方:{{ countSqu }}</h5>
<Me></Me>
</div>
</template>
<script>
import { mapState, mapActions, mapGetters } from 'vuex'
import Me from './components/Me.vue'
export default {
components: { Me },
computed: {
// count() {
//
return this.$store.state.count
// },
...mapState(['uname', 'count']),
...mapGetters(['countSqu']),
},
methods: {
...mapActions(['asyncAddCount']),
//同步操作用$store.commitcommit
addCount() {
this.$store.commit('addCount', 10)
},
delCount() {
this.$store.commit('delCount', 5)
},
//触发异步操作: 用$store.dispatch
(定时器 ajax处理)
asyncadd() {
// this.$store.dispatch('asyncAddCount', 6) //每次加6 ,
this.asyncAddCount(4) //每次加444 用mapActions 调用
},
},
created() {
console.log(this)
},
}
</script>
<style></style>
import Vue from 'vue'
import App from './App.vue'
import Vuex from 'vuex'
Vue.config.productionTip = false
Vue.use(Vuex) //让vue使用Vuex
//创建一个实例
const store = new Vuex.Store({
state: {
uname: 'ha',
count: 0,
word: '',
},
getters: {
// countSqu(state) {
//
console.log(state)
//
return state.count ** 2
// },
countSqu: (state) => state.count ** 2,
},
//state指的式state的里面的count参数,payload值是传入的每次加的值
// mutations: 所有共享的数据通过mutations运行工作
mutations: {
//同步
addCount(state, payload = 1) {
state.count += payload
},
delCount(state, payload = 1) {
state.count -= payload
},
},
actions: {
//异步 通过actions调用mutation里面的addcount,然后改变count里面的数据
//payload接收传来的参数
asyncAddCount(context, payload) {
console.log('context', context)
setTimeout(() => {
context.commit('addCount', payload)
}, 1000)
},
},
})
new Vue({
store, //让Vue的实例使用创建的store实例
render: (h) => h(App),
}).$mount('#app')
最后
以上就是隐形眼神为你收集整理的Vuex组件通讯的全部内容,希望文章能够帮你解决Vuex组件通讯所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复