import {INCREMENT} from "./types"
const mutations = {
[INCREMENT] (state) {
state.count++;
}
}
[INCREMENT]
INCREMENT是變數直接使用不就行了嗎,為什麼還要加一個中括號呢?
黄舟2017-05-16 13:44:12
[INCREMENT]
是计算INCREMENT
这个变量的值作为函数名,不使用中括号是把INCREMENT
這個字串作為函數名。
const INCREMENT = 'myfunc';
const mutations = {
[INCREMENT] (state) {
state.count++;
}
}
相當於上面的程式碼,結果是
const mutations = {
myfunc(state) {
state.count++;
}
}
而
const INCREMENT = 'myfunc';
const mutations = {
INCREMENT (state) {
state.count++;
}
}
的結果是
const mutations = {
INCREMENT(state) {
state.count++;
}
}