VueX源码分析(3)
VueX源码分析(3)
还剩余
/module
/plugins
store.js
/plugins/devtool.js
const devtoolHook =
typeof window !== 'undefined' &&
window.__VUE_DEVTOOLS_GLOBAL_HOOK__
export default function devtoolPlugin (store) {
if (!devtoolHook) return
store._devtoolHook = devtoolHook
devtoolHook.emit('vuex:init', store)
devtoolHook.on('vuex:travel-to-state', targetState => {
store.replaceState(targetState)
})
store.subscribe((mutation, state) => {
devtoolHook.emit('vuex:mutation', mutation, state)
})
}
- 浏览器开发者工具支持监控VueX
/plugins/logger.js
repeat
function repeat (str, times) {
return (new Array(times + 1)).join(str)
}
解析:
- 字符串重复几次repeat('0', 5)会返回'00000'
pad
function pad (num, maxLength) {
return repeat('0', maxLength - num.toString().length) + num
}
解析:
- 给数字补零,如pad(5, 5) 会返回'000005'
createLogger
import { deepCopy } from '../util'
export default function createLogger ({
collapsed = true,
filter = (mutation, stateBefore, stateAfter) => true,
transformer = state => state,
mutationTransformer = mut => mut,
logger = console
} = {}) {
return store => {
let prevState = deepCopy(store.state)
store.subscribe((mutation, state) => {
if (typeof logger === 'undefined') {
return
}
const nextState = deepCopy(state)
if (filter(mutation, prevState, nextState)) {
const time = new Date()
const formattedTime = ` @ ${pad(time.getHours(), 2)}:${pad(time.getMinutes(), 2)}:${pad(time.getSeconds(), 2)}.${pad(time.getMilliseconds(), 3)}`
const formattedMutation = mutationTransformer(mutation)
const message = `mutation ${mutation.type}${formattedTime}`
const startMessage = collapsed
? logger.groupCollapsed
: logger.group
// render
try {
startMessage.call(logger, message)
} catch (e) {
console.log(message)
}
logger.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState))
logger.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation)
logger.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState))
try {
logger.groupEnd()
} catch (e) {
logger.log('—— log end ——')
}
}
prevState = nextState
})
}
}
console
的一些报错处理模板store.subscribe
监控的是commit
,每次commit都会执行一次检测
VueX源码分析(3)的更多相关文章
- VueX源码分析(5)
VueX源码分析(5) 最终也是最重要的store.js,该文件主要涉及的内容如下: Store类 genericSubscribe函数 resetStore函数 resetStoreVM函数 ins ...
- VueX源码分析(4)
VueX源码分析(4) /module store.js /module/module.js import { forEachValue } from '../util' // Base data s ...
- VueX源码分析(2)
VueX源码分析(2) 剩余内容 /module /plugins helpers.js store.js helpers要从底部开始分析比较好.也即先从辅助函数开始再分析那4个map函数mapSta ...
- VueX源码分析(1)
VueX源码分析(1) 文件架构如下 /module /plugins helpers.js index.esm.js index.js store.js util.js util.js 先从最简单的 ...
- 逐行粒度的vuex源码分析
vuex源码分析 了解vuex 什么是vuex vuex是一个为vue进行统一状态管理的状态管理器,主要分为state, getters, mutations, actions几个部分,vue组件基于 ...
- vuex源码分析3.0.1(原创)
前言 chapter1 store构造函数 1.constructor 2.get state和set state 3.commit 4.dispatch 5.subscribe和subscribeA ...
- vuex 源码分析(七) module和namespaced 详解
当项目非常大时,如果所有的状态都集中放到一个对象中,store 对象就有可能变得相当臃肿. 为了解决这个问题,Vuex允许我们将 store 分割成模块(module).每个模块拥有自己的 state ...
- vuex 源码分析(六) 辅助函数 详解
对于state.getter.mutation.action来说,如果每次使用的时候都用this.$store.state.this.$store.getter等引用,会比较麻烦,代码也重复和冗余,我 ...
- vuex 源码分析(五) action 详解
action类似于mutation,不同的是Action提交的是mutation,而不是直接变更状态,而且action里可以包含任意异步操作,每个mutation的参数1是一个对象,可以包含如下六个属 ...
随机推荐
- webpack配置Jquery全局包及全局包插件
一:在配置文件配置: plugins: [ //将来以template为模版,生成一个index.html并且发布到webpack-dev-server开启的node服务器上面去 new HtmlWe ...
- linux下find查找与批量替换文件中指定内容
经常在部署tomcat时需要替换配置文件中的ip,find命令批量替换还是很方便的 查找需要替换的ip,看看哪些文件有配置这个ip,执行下面命令: find ./ -type f -regex &qu ...
- Decorator模式(装饰器模式)
Decorator模式? 假如现在有一块蛋糕,如果只涂上奶油,其他什么都不加,就是奶油蛋糕.如果加上草莓,就是草莓奶油蛋糕.如果再加上一块黑色巧克力板,上面用白色巧克力写上姓名,然后插上代表年龄的蜡烛 ...
- HDU-2588-GCD (欧拉函数)
The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the ...
- spring动态线程池(实质还是用了java的线程池)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- DB2 触发器使用1
本文基于多篇博文整理而来,目的是较全面的学会使用DB2触发器,后期再整理复杂的使用场景,看完本文应该能够自己创建一个基本的触发器. 1.什么是触发器当一个指定的 SQL 操作(如 DELETE,INS ...
- JS——通过标签获取元素、循环语句、数组去重
通过标签获取元素: var aLi = document.getElementsByTagName('Li'); aLi[0].style.backgroundColor = 'gold'; 选中部分 ...
- Spring学习(六)整合web开发
https://www.cnblogs.com/Leo_wl/p/4459274.html 1.加载Spring核心配置文件 //1.加载Spring配置文件,根据创建对对象 ApplicationC ...
- Shell笔试题3
1.查找当前目录中所有大于500M的文件,把这些文件名写到一个文本文件中,并统计其个数.find ./ -size +500M -type f | tee file_list | wc -l 2.在目 ...
- sql 删除字段 出错
1. 删除字段: ALTER TABLE TALE_NAME DROP COLUMN nn; 2.报错: 消息 5074,级别 16,状态 1,第 1 行对象'DF__WorkOrder__I ...