V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
matts8023

为什么直接绑定在 Vue 自定义组件标签上的事件无法触发

  •  
  •   matts8023 · Feb 7, 2017 · 13709 views
    This topic created in 3367 days ago, the information mentioned may be changed or developed.

    如题,我在自定义组件标签上绑定了事件,点击时却无法触发,代码如下:

    
    <template id="test">
      <p>Hi</p>
    </template>
    
    <div id="app">
      <test @click="hello"></test>
    </div>
    
    
    
    Vue.component('test', {
      template: "#test"
    })
    
    new Vue({
      el: '#app',
      data: {},
      methods: {
        hello: function() {
          alert();
        }
      }
    })
    
    

    然鹅,我把 @click 放到父级标签上却又可以触发了,这是为啥?

    
    <div id="app" @click="hello">
      <test></test>
    </div>
    
    
    5 replies    2017-02-07 20:36:52 +08:00
    zhuangtongfa
        1
    zhuangtongfa  
       Feb 7, 2017   ❤️ 1
    @click.native  试试
    matts8023
        2
    matts8023  
    OP
       Feb 7, 2017
    @zhuangtongfa 嗯,刚刚在 Vue 官方库的 issue 中找到,可以用,谢谢 O(∩_∩)O
    kqz901002
        3
    kqz901002  
       Feb 7, 2017
    @matts8023 icon
    matts8023
        4
    matts8023  
    OP
       Feb 7, 2017
    @kqz901002 哈哈哈哈哈
    yunkou
        5
    yunkou  
       Feb 7, 2017
    官方文档有说明哦
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1038 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 22:05 · PVG 06:05 · LAX 15:05 · JFK 18:05
    ♥ Do have faith in what you're doing.