vue3的事件处理代码

第一次的思考
引用的一定要改,从2.5的vue改为3以上的

<!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>

第二次思考:修改了mount()里的,改为app
第三次思考:修改了methods下的greet,改为test,遵照在@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>

猜你喜欢

转载自blog.csdn.net/weixin_40945354/article/details/115241754