Vue3 event handling code

The first thought, the
reference must be changed, from vue 2.5 to 3 or more

<!DOCTPYE html>
<html>
    <head>
        <title>ruguog</title>
    </head> 
    
<body>
    <div id="app">
        <input type="button" value="hamimelon!" @click="test"/>
    </div>


<script src="https://unpkg.com/vue@next"></script>
<script>
Vue.createApp({
    
    
  data() {
    
    
    return {
    
    
      name: 'Vue.js'
    }
  },
  methods: {
    
    
    greet(event) {
    
    
      // `methods` 内部的 `this` 指向当前活动实例
      alert('Hello ' + this.name + '!')
      // `event` 是原生 DOM event
      if (event) {
    
    
        alert(event.target.tagName)
      }
    }
  }
}).mount('#event-with-method')
</script>
</body>
</html>

Second thought: modified mount() and changed it to app.
Third thought: modified greet under methods and changed to test, following the instructions in @click.

<!DOCTPYE html>
<html>
    <head>
        <title>ruguog</title>
    </head> 
<body>
    <div id="app">
        <input type="button" value="hamimelon!" @click="test"/>
    </div>
<script src="https://unpkg.com/vue@next"></script>
<script>
Vue.createApp({
    
    
  data() {
    
    
    return {
    
    
      name: 'Vue.js'
    }
  },
  methods: {
    
    
    test(event) {
    
    
      // `methods` 内部的 `this` 指向当前活动实例
      alert('Hello ' + this.name + '!')
      // `event` 是原生 DOM event
      if (event) {
    
    
        alert(event.target.tagName)
      }
    }
  }
}).mount('#app')
</script>
</body>
</html>

Guess you like

Origin blog.csdn.net/weixin_40945354/article/details/115241754