我在 vuex 文档和 redux 中看到过很多次使用箭头函数作为对象方法,但我不明白为什么。为什么不使用常规对象方法,因为商店是一个对象?是不是因为在引擎盖下使用了代理而无关紧要?
在
vue
:
const store = createStore({
data: () => ({
count: 0
}),
methods: {
increment(state) {
state.count++
}
}
})
在
redux
:
const counterSlice = createSlice({
name: 'counter',
initialState: {
value: 0
},
reducers: {
incremented: state => {
state.value += 1
}
}
})