Heim > Fragen und Antworten > Hauptteil
import {INCREMENT} from "./types"
const mutations = {
[INCREMENT] (state) {
state.count++;
}
}
[INCREMENT]
INCREMENT ist eine Variable, die direkt verwendet werden kann. Warum müssen wir eine Klammer hinzufügen?
黄舟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++;
}
}