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

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.

主站蜘蛛池模板: 静乐县| 灌阳县| 临沭县| 镇平县| 正宁县| 镇雄县| 新昌县| 庄浪县| 孝昌县| 积石山| 无棣县| 舒城县| 浪卡子县| 济宁市| 山丹县| 宣城市| 丽水市| 三亚市| 友谊县| 诸城市| 云阳县| 广安市| 安徽省| 磐安县| 绥芬河市| 江西省| 桂平市| 余庆县| 武安市| 石柱| 抚宁县| 巩义市| 海阳市| 青岛市| 广昌县| 石台县| 呼图壁县| 开远市| 隆昌县| 寿宁县| 永年县|