自学内容网 自学内容网

vue中绑定

在vue当中,要给元素绑定事件需要用到vue指令,指令一般以v-开头,例如绑定单击事件的指令是v-on:click = “函数名”,简写为@click = “函数名”

例如以下例子:单击button按钮,执行showInfo函数,弹出alert窗口。

点击button按钮

<body>
  <button class="btn" v-on:click = "showInfo">点击弹出{{name}}窗口</button>
  <script type="text/javascript">
     Vue.config.productionTip = false;
     const vm = new Vue({
      data:{
        name:"alert"
      },
      methods:{
        showInfo(){
          alert('hello!');
        }
      }
     })
     vm.$mount('.btn')
  </script>
</body>

 

vm.$mount(".btn")指定了可供vue操作的元素,与el关键字的作用类似,但比el更加灵活。

methods记录了所有的方法,凡事与vue所操作元素有关的方法都应该写在method里面,否则无效。

在button属性里面,使用v-on:click绑定了一个单击事件,当点击此对象时,就执行showInfo函数。

默认参数为event

<body>
  <button class="btn" v-on:click = "showInfo">点击弹出{{name}}窗口</button>
  <script type="text/javascript">
     Vue.config.productionTip = false;
     const vm = new Vue({
      data:{
        name:"alert"
      },
      methods:{
        showInfo(a,b,c){
          alert('hello!');
          console.log(a,b,c);
        }
      }
     })
     vm.$mount('.btn')
  </script>
</body>

 


原文地址:https://blog.csdn.net/weixin_62980497/article/details/137447764

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!