当前位置:网站首页>Issue 37: mapstate analysis
Issue 37: mapstate analysis
2022-07-18 01:46:00 【terrence386】
About Vuex Source code , I feel it is necessary to study it carefully . Look at the code written by others , Then look at the code you usually write , I don't feel at the same level at all .
With Vuex In the source mapState Methods as an example :
export const mapState = normalizeNamespace((namespace, states) => {
const res = {}
if (__DEV__ && !isValidMap(states)) {
console.error('[vuex] mapState: mapper parameter must be either an Array or an Object')
}
normalizeMap(states).forEach(({ key, val }) => {
res[key] = function mappedState () {
let state = this.$store.state
let getters = this.$store.getters
if (namespace) {
const module = getModuleByNamespace(this.$store, 'mapState', namespace)
if (!module) {
return
}
state = module.context.state
getters = module.context.getters
}
return typeof val === 'function'
? val.call(this, state, getters)
: state[val]
}
// mark vuex getter for devtools
res[key].vuex = true
})
return res
})
It uses here normalizeNamespace Method . The function of this method is to return a function , Accept namespace and map As a parameter .
function normalizeNamespace (fn) {
return (namespace, map) => {
if (typeof namespace !== 'string') {
map = namespace
namespace = ''
} else if (namespace.charAt(namespace.length - 1) !== '/') {
namespace += '/'
}
return fn(namespace, map)
}
}
then mapState Method is actually equal to normalizeNamespace Method returns the last fn(namespace, map).
That's the code below :
(namespace, states) => {
const res = {}
if (__DEV__ && !isValidMap(states)) {
console.error('[vuex] mapState: mapper parameter must be either an Array or an Object')
}
normalizeMap(states).forEach(({ key, val }) => {
res[key] = function mappedState () {
let state = this.$store.state
let getters = this.$store.getters
if (namespace) {
const module = getModuleByNamespace(this.$store, 'mapState', namespace)
if (!module) {
return
}
state = module.context.state
getters = module.context.getters
}
return typeof val === 'function'
? val.call(this, state, getters)
: state[val]
}
// mark vuex getter for devtools
res[key].vuex = true
})
return res
}
This function will first put states Into normalized Map, And then we're going to iterate , redefined res object , Traversal Map Of key Value corresponding value Assign to res object , Finally, this function returns res object .
Do not consider intermediate calls call The process of , It's just mapState Equal to a function , Function finally returns the operation of an object , The process of writing in this way , It seems that I seldom write this myself .
What I wrote , At most, some commonly used , Or use more methods to draw a method , It is possible to receive different parameters , Then return different values , And rarely used call This method .
mapState To put it bluntly, it is still a function , This function will traverse by state Object into Map, Then reassemble it into a new object to return , It's just that when recombining, we will judge whether the corresponding value of each building is a function , If it's a function , Then update the value of this key to the value after the function is executed .
In turn ,mapActions,mapMutations Something like this should be similar .
边栏推荐
猜你喜欢
随机推荐
第四十四期:高效团队养成
【C 练习】下列代码输出的结果是什么
Matlab中响应鼠标的事件
STM32 application development practice tutorial: application development of persistent storage of environmental parameters
Common vulnerability types of tcp/ip protocol
Matlab low-level source code realizes Prewitt edge detection and Sobel, Laplace edge detection (the implementation effect is consistent with Halcon)
Cloud mall source code repair 30 sets of templates to support one click docking of major systems
第五十三期:彻底理解MVC,MVP和MVVM
Mysql事务
云服务器跟物理机哪个更适合做高防服务器
【C】 Address of array
【C 练习】打印‘X’图形
【C数据的存储】
【C】const 修饰指针
Matrix fast power
STM32应用开发实践教程:环境温湿度监测的应用开发
送你的代码上太空,与华为云一起开发“最伟大的作品”
[C exercise] print all "Narcissus numbers" from 0 to 100000
现在网上开户安全么?想咨询一下,国债逆回购是什么?
【C 练习】输入年月,求出月份天数









