九、vue中事件对象的获取

事件对象的获取

事件对象的获取分为两种情况,分别是事件方法加括号和不加括号

  1. 不加括号
<body>
  <div id="root">
    <button @click="handleClick">添加button>
  div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
  <script>
    const app = new Vue({
      el: "#root",
      data() {
        return {
          hobi: []
        }
      },

      methods: {
        handleClick(e) {
          console.log(e)
        }
      }
    })
  script>
body>
  1. 加括号

加括号是为了可以传参

<body>
  <div id="root">
    <button @click="handleClick('abc', $event)">添加button>
  div>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js">script>
  <script>
    const app = new Vue({
      el: "#root",
      data() {
        return {
          hobi: []
        }
      },

      methods: {
        handleClick(desc, e) {
          console.log(desc, e)
        }
      }
    })
  script>
body>

你可能感兴趣的:(vue,事件对象获取)