vue-learning:41 - Vuex - 第二篇:const store = new Vue.Store(option)中option选项、store实例对象的属性和方法
vuex 第二篇:const store = new Vue.Store(option)中option选项、store实例对象的属性和方法
import Vuex from 'vuex'
const store = new Vuex.Store(option)
- Vuex对象
- option选项
- store实例对象的属性
- store实例对象的方法
Vuex 对象
在使用Vuex
时,看下Vuex
对象提供了哪些属性和方法。
// vuex源码入口index.js
export default {
Store, // 仓库store的构造函数
install, // 提供vue.use(vuex)使用的插件安装入口
version: '__VERSION__',
// store对象属性和方法的辅助函数,后面讲到
mapState,
mapMutations,
mapGetters,
mapActions,
createNamespacedHelpers
}
option配置选项
const option = {
state, // Object | Function
getters, // { [key: string]: Function(state, getters, rootState, rootGetters ) }
mutations, // { [type: string]: Function(state, paylaod) }
actions, // { [type: string]: Function(context, payload) } context= {state,rootState, getters, rootGetters, commit, dispatch }
modules, // {key: {namespaced?, state, mutations, getters?, actions?, modules?}}
plugins, // Array<Function(store)>
strict, // Boolean,默认false
devtools, // Boolean,默认false
}
实例对象store的属性
const store = new Vuex.Store(...option)
// 只读属性,调用已option中注册的state 和 getters
store.state
store.getters
实例对象store的方法
commit()
| dispatch()
store.commit(type: string, payload?: any, options?: Object) // commit(mutation: Object, options?: Object),即对象形式commit({type:string, payload?:any}, {root:ture})
store.dispatch(type: string, payload?: any, options?: Object) // dispatch(action: Object, options?: Object),即对象形式dispatch({type:string, payload?:any}, {root:ture})
// 如果定义了一个action返回的是promise对象,那么dispatch这个action后返回的仍是一个promise对象
actions: {
actionA ({ commit }) {
return new Promise((resolve, reject) => {
setTimeout(() => {
commit('someMutation')
resolve()
}, 1000)
})
}
}
store.dispatch('actionA').then(() => {
// ...
})
注册和卸载plugin的方法
// 订阅mutation的变化。handler 会在每个 mutation 完成后调用。 如果取消订阅,调用返回函数unsubscribe()
const unsubscribe = store.subscribe(handler(mutation,state){})
const unsubscribeAction = store.subscribeAction(handler(action, state){})
const unsubscribeAction = store.subscribeAction({before(action,state){},after(action,state){}})
// 定义一个订阅mutation的插件
const myPlugin = store => {
// 当 store 初始化后调用
store.subscribe((mutation, state) => {
// 每次 mutation 之后调用
// mutation 的格式为 { type, payload }
})
}
// 在store中注册自定义插件
const store = new Vuex.Store({
// ...
plugins: [myPlugin]
})
注册和卸载模块module的方法
// 注册模块
// Array<string> 注册嵌套模块 `nested/myModule`: ['nested', 'myModule']
// Module是一个对象,包含正常{key: {namespaced?, state, mutations, getters?, actions?, modules?}}
// options可以包含 preserveState: true 以允许保留之前的 state。用于服务端渲染
store.registerModule(path: string | Array<string>, module: Module, options?: Object)
// 卸载模块
store.unregisterModule(path: string | Array<string>)
vuex的辅助函数
// 在组件中引入
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex'
// 使用
mapState(namespace?: string, map: Array<string> | Object<string | function>): Object
mapGetters(namespace?: string, map: Array<string> | Object<string>): Object
mapMutations(namespace?: string, map: Array<string> | Object<string | function>): Object
mapActions(namespace?: string, map: Array<string> | Object<string | function>): Object
示例:
import { mapState } from 'vuex'
export default {
// ...
computed: {
// Array<string>:当映射的计算属性的名称与 state 的子节点名称相同时,我们也可以给 mapState 传一个字符串数组
...mapState(namespace?: string, ['count'])
// Object<string | function>对象形式
...mapState(namespace?: string, {
// 传字符串参数 'count' 等同于 `state => state.count`
countAlias: 'count',
// 函数形式,接收state作为参数
count: state => state.count,
// 为了能够使用 `this` 获取局部状态,必须使用常规函数
countPlusLocalState (state) {
return state.count + this.localCount
}
})
}
}
import { mapGetters } from 'vuex'
export default {
// ...
computed: {
// 使用对象展开运算符将 getter 混入 computed 对象中
...mapGetters(namespace?: string,[
'doneTodosCount',
'anotherGetter',
// ...
]),
// 如果你想将一个 getter 属性另取一个名字,使用对象形式,只有key:string,没有function函数形式
...mapGetters(namespace?: string,{
// 把 `this.doneCount` 映射为 `this.$store.getters.doneTodosCount`
doneCount: 'doneTodosCount'
})
}
}
import { mapMutations } from 'vuex'
export default {
// ...
methods: {
...mapMutations(namespace?: string,[
'increment', // 将 `this.increment()` 映射为 `this.$store.commit('increment')`
// `mapMutations` 也支持载荷:
'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.commit('incrementBy', amount)`
]),
...mapMutations(namespace?: string,{
add: 'increment' // 将 `this.add()` 映射为 `this.$store.commit('increment')`
})
}
}
import { mapActions } from 'vuex'
export default {
// ...
methods: {
...mapActions(namespace?: string,[
'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`
// `mapActions` 也支持载荷:
'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`
]),
...mapActions(namespace?: string,{
add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
})
}
}
vuex项目组织
参考链接
vuex源码分析
小火柴的蓝色理想:Vue状态管理vuex
前端状态管理与有限状态机
vue-learning:41 - Vuex - 第二篇:const store = new Vue.Store(option)中option选项、store实例对象的属性和方法的更多相关文章
- vue第六单元(vue的实例和组件-vue实例的相关属性和方法-解释vue的原理-创建vue的组件)
第六单元(vue的实例和组件-vue实例的相关属性和方法-解释vue的原理-创建vue的组件) #课程目标 掌握vue实例的相关属性和方法的含义和使用 了解vue的数据响应原理 熟悉创建组件,了解全局 ...
- Vue学习之路第二篇:插值表达式
要开始写Vue的功能了,是不是很激动呢!开始吧! 1.首先建立一个html页面,导入Vue js包 <script type="text/javascript" src=&q ...
- vue.js学习系列-第二篇
一 VUE实例生命周期钩子 1 生命周期函数 定义 生命周期函数就是vue在某一时间点自动执行的函数 2 具体函数 1 new vue() 2 before ...
- vue学习指南:第二篇(详细Vue基础) - Vue的指令
一. Vue 的介绍 1. vue是一个 mvvm 的框架.(面试官经常会问的),angular 是 mvc的框架. 2. vm 是 vum 的实例,这个实例存在计算机内存中,主要干两件大事: 1. ...
- Vue.js学习笔记 第二篇 样式绑定
Class绑定的对象语法 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- Matlab高级教程_第二篇:关于MATLAB转C#过程中遇到输出两组参数的问题
1. 在matlab的m函数很可能遇到原函数[a,b] = func(a); 这样的两个输出参数. 2. 在观察C#生成后定义中我们发现: public MWArray HP(); public MW ...
- Matlab高级教程_第二篇:关于MATLAB转C#过程中MWArray到C#数组,C#数组到MWArray相互转换
Matlab传递数据时使用的方法,那么Matlab计算完成后在C#中应该怎么获取它的计算数据呢? 需要遵循两个基本步骤: 弄清楚Matlab函数传回的数据到底是什么格式?struct?cell?cha ...
- Vue状态管理vuex
前面的话 由于多个状态分散的跨越在许多组件和交互间各个角落,大型应用复杂度也经常逐渐增长.为了解决这个问题,Vue提供了vuex.本文将详细介绍Vue状态管理vuex 引入 当访问数据对象时,一个 V ...
- python 全栈开发,Day93(vue内容补充,VueX)
昨日内容回顾 1. 页面的布局 Vue中使用Bootstrap搭页面 1. 安装 1. npm install bootstrap@3.3.7 -S 2. 使用 1. import 'bootstra ...
随机推荐
- poj1637&&hdu1956 混合欧拉回图判断
欧拉路:经过所有路有且仅有1次,可以路过所有的点. 无向图: 图连通,所有点都是偶数度,或者只有两个点是奇数度.当所有点是偶数度时欧拉路起点可以是任意点:当有两个奇数度点时起点必须是奇数度点. 有向 ...
- oralce update操作
1.基本语法:update 表名 set 列名=表达式 [列名=表达式. . . ] where 条件 2.使用的注意事项: v UPDATE语法可以用新值更新原有表行中的各列 把zs的性别改为女 ...
- HDU 5673 Robot【卡特兰数】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5673 题意: 有一个机器人位于坐标原点上.每秒钟机器人都可以向右移到一个单位距离,或者在原地不动.如 ...
- C-链表实现,保存文件,评估-单项选择题系统课程设计---ShinePans
课程设计 单项选择题标准化考试系 所属专业:软件project软件三班 完毕人:潘尚 一.设计计划. 1.能够用菜单明白的指导用户操作. 2.操作完毕能够返回主菜单. 3.将输入的题目保存至C盘的 ...
- 荣获“5G MEC优秀商用案例奖”,阿里云边缘计算发力新零售
4月24日,在中国联通合作伙伴大会的 “5G MEC(Mobile Edge Computing,移动边缘计算)边缘云赋能行业数字化转型”分论坛上,阿里云“基于5G边缘计算的新零售应用案例”荣获201 ...
- CSS长度单位:px和pt的区别
先搞清基本概念:px就是表示pixel,像素,是屏幕上显示数据的最基本的点:而pt就是point,是印刷行业常用单位,等于1/72英寸. 这样很明白,px是一个点,它不是自然界的长度单位,谁能说出一个 ...
- 谷歌BERT预训练源码解析(三):训练过程
目录前言源码解析主函数自定义模型遮蔽词预测下一句预测规范化数据集前言本部分介绍BERT训练过程,BERT模型训练过程是在自己的TPU上进行的,这部分我没做过研究所以不做深入探讨.BERT针对两个任务同 ...
- 让 AE 输出 MPEG
最近在做视频后期处理,但是我发现 AE 的文件都很大,大概一个 10 分钟视频 10G ,所以有什么办法让他输出的文件变小?一个方法是使用 MPEG 输出. 本文告诉大家如何让 AE 输出 MPEG ...
- oracle CBO下使用更具选择性的索引
基于成本的优化器(CBO, Cost-Based Optimizer)对索引的选择性进行判断来决定索引的使用是否能提高效率. 如果索引有很高的选择性, 那就是说对于每个不重复的索引键值,只对应数量很少 ...
- 强连通分量-----Kosaraju
芝士: 有向图强连通分量在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶点强连通(strongly connect ...