Vue 'methods' 选项
实例
使用 methods 选项中的方法来切换消息。
export default {data() {return {msg: 'Hello World!',showMsg: false};},methods: {toggleMsg() {this.showMsg = !this.showMsg;}}};
定义与用法
methods option 选项是一个包含 Vue 实例上声明的所有方法的对象。
可以从 Vue 应用程序的 <template> 直接调用方法(不使用 this 关键字),例如,当一个方法设置为在事件发生时运行时,使用 v-on 指令。
this 关键字必须用于从 Vue 实例中调用方法,例如当一个方法被另一个方法调用时。
注意:在声明方法时应避免使用箭头函数,因为使用 this 关键字无法从此类函数内部访问 Vue 实例。
关联页面
Vue 教程: Vue 方法
Vue 教程: Vue v-on 指令