美文网首页
10.给组件绑定原生事件

10.给组件绑定原生事件

作者: yaoyao妖妖 | 来源:发表于2018-07-07 17:54 被阅读2次

1.如何给组件绑定原生事件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>组件</title>
    <!--引入/vue.js-->
    <script src="./vue.js"> </script>
</head>
<body>
   <div id="root">
       <!-- 父组件绑定事件想要触发,必须在子组件中向外触发事件 -->
       <child @click="fuHandleClick">Child</child>  
   </div>

   <script>

       Vue.component('child',{
           //子组件绑定事件直接触发即可,
           template:'<div @click="childhandleClick">child</div>',
           methods:{
            childhandleClick:function(){
                alert("childhandleClick");
                this.$emit('click')//向哪个事件抛出
            }
           }
       })
 
       var app = new Vue({
           el:'#root', 
           methods:{
            fuHandleClick:function(){
                alert('fuHandleClick')

            }
           }              
       })

   </script>

</body>

</html>

2.效果同一,只需要给组件添加native修饰符即可

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>组件</title>
    <!--引入/vue.js-->
    <script src="./vue.js"> </script>
</head>
<body>
   <div id="root">
       <!-- 父组件绑定事件想要触发,必须在子组件中向外触发事件 -->
       <child @click.native="fuHandleClick">Child</child>  
   </div>

   <script>

       Vue.component('child',{
           //子组件绑定事件直接触发即可,
           template:'<div>child</div>'
       })
 
       var app = new Vue({
           el:'#root', 
           methods:{
            fuHandleClick:function(){
                alert('fuHandleClick')

            }
           }      
         
       })

   </script>

</body>

</html>

相关文章

网友评论

      本文标题:10.给组件绑定原生事件

      本文链接:https://www.haomeiwen.com/subject/jqdguftx.html