method 注意事项

  1. data 中的属性和 method 中的属性不能重名;
  2. method 中的函数中的 this 是当前 vue 实例;
  3. 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>
打赏作者
您的打赏是我前进的动力
微信
支付宝
评论

中午好👏🏻,我是 ✍🏻   疯狂 codding 中...

粽子

这有关于前端开发的技术文档和你分享。

相信你可以在这里找到对你有用的知识和教程。

了解更多

目录

  1. 1. method 注意事项
  2. 2. 示例代码