官术网_书友最值得收藏!

Listening to native events on components

Let's try adding a click event handler on our card:

<card :def="testCard" @click="handlePlay" />

With a dumb method in the main component:

methods: {
handlePlay () {
console.log('You played a card!')
}
}

If you test this in the browser, you may be surprised that it doesn't work as expected. Nothing is output to the console...

This is because Vue has its own event system for components, called "custom events", that we will learn about in a moment. This system is separate from the browser events, so here Vue expects a custom 'click' event, and not the browser one. Thus, the handler method is not called.

To get around this, you should use the .native modifier on the v-on directive, as follows:

<card :def="testCard" @click.native="handlePlay" />

Now, the handlePlay method is called when you click on the card, as expected.

主站蜘蛛池模板: 奎屯市| 酉阳| 周口市| 台东市| 望谟县| 松阳县| 土默特左旗| 历史| 鹤峰县| 赤城县| 都江堰市| 营口市| 新化县| 体育| 双峰县| 中西区| 金门县| 贡觉县| 苍溪县| 麻阳| 衡阳市| 大厂| 徐州市| 乐亭县| 黄浦区| 揭阳市| 普格县| 庆阳市| 鄂州市| 紫阳县| 林口县| 五指山市| 黑龙江省| 鄢陵县| 贞丰县| 永仁县| 利津县| 忻州市| 吐鲁番市| 东方市| 饶河县|