Home > Web Front-end > Vue.js > body text

How to bind functions in vuejs

青灯夜游
Release: 2023-01-13 00:45:32
Original
4318 people have browsed it

How to bind functions in vuejs: 1. Use the form of "

How to bind functions in vuejs

The operating environment of this tutorial: windows7 system, vue version 2.9.6, DELL G3 computer.

Event binding in vuejs is done using <v-on: event name = function name>, where the function name is defined in the methods object in the Vue instance Yes, the Vue instance can directly access the methods.

Event binding method

(1) Write js directly inline Call the method

 <button v-on:click="alert(&#39;hi&#39;)">执行方法的第一种写法</button>
Copy after login

on the label (2) Call the method defined in methods

      <button v-on:click="run()">执行方法的第一种写法</button>
      <button @click="run()">执行方法的 简写 写法</button>
Copy after login
    export default {     
      data () { 
        return {
          msg: &#39;你好vue&#39;,
          list:[]      
        }
      },
      methods:{
           run:function(){
              alert(&#39;这是一个方法&#39;);
             }
           }
 }
Copy after login

method passing parameters , the method is directly passed in the method when calling Parameters

      <button @click="deleteData(&#39;111&#39;)">执行方法传值111</button>

      <button @click="deleteData(&#39;222&#39;)">执行方法传值2222</button>
Copy after login
        deleteData(val){
            alert(val);
        },
Copy after login

Incoming event object

      <button data-aid=&#39;123&#39; @click="eventFn($event)">事件对象</button>
Copy after login
        eventFn(e){
          console.log(e);
          // e.srcElement  dom节点
          e.srcElement.style.background=&#39;red&#39;;
          console.log(e.srcElement.dataset.aid);  /*获取自定义属性的值*/
        }
Copy after login

Related recommendations: "vue.js Tutorial"

The above is the detailed content of How to bind functions in vuejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
vue
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!