method 注意事项
- data 中的属性和 method 中的属性不能重名;
- method 中的函数中的 this 是当前 vue 实例;
- method 中的方法会在原型作用域中去寻找,所以 method 不能定义箭头函数;
示例代码
<template>
<div>
<!-- 输出 event对象 -->
<buttom @click='fn'></button>
<!-- 输出 undefined -->
<buttom @click='fn()'></button>
<!-- 输出 123 和 事件对象 -->
<buttom @click='fn(123,$event)'></button>
</div>
</template>
<script type="text/javascript">
const vm = new Vue({
el: '#demo',
data: {
firstName: 'A',
lastName: 'B',
fullName2: 'A-B',
},
method: {
fn(...arg) {
// 此处的 this 是当前实例
console.log(arg);
},
},
});
</script>
打赏作者
您的打赏是我前进的动力
微信
支付宝
vue2.x✍️ v-model 和 sync 修饰符
上一篇
评论