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

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.

主站蜘蛛池模板: 乌拉特中旗| 柳江县| 阳城县| 延庆县| 咸丰县| 晋宁县| 廊坊市| 牟定县| 朝阳区| 苏尼特左旗| 怀集县| 肃宁县| 红桥区| 海宁市| 肥城市| 衡山县| 慈利县| 连云港市| 平原县| 五台县| 东城区| 平安县| 开化县| 凌海市| 中阳县| 班戈县| 南康市| 通辽市| 炉霍县| 修水县| 景东| 错那县| 微山县| 海晏县| 哈尔滨市| 唐海县| 吕梁市| 怀来县| 普兰县| 阿拉善右旗| 宁阳县|