通常业务代码中会有这样的逻辑:
if(type === 'a') {
dispatch('someAction', {
params: {
a: 1
}
})
}
if(type === 'b') {
dispatch('someAction', {
params: {
b: 2
}
})
}
if(type === 'c') {
dispatch('someAction', {
params: {
a: 1,
b: 2
}
})
}
前面两个好解决,应用策略模式可以简化,但有多个对象如何优化呢?
const valueObj = {
a: 1,
b: 2
}
dispatch('someAction', {
params: {
[type]: valueObj[type]
}
})