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

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.

主站蜘蛛池模板: 贞丰县| 搜索| 司法| 青阳县| 县级市| 丰都县| 淮阳县| 肃南| 清远市| 焦作市| 邢台市| 贵溪市| 安新县| 林甸县| 长海县| 册亨县| 香河县| 四平市| 南川市| 手机| 佳木斯市| 宜良县| 新巴尔虎左旗| 道孚县| 延庆县| 塔河县| 新河县| 泰州市| 区。| 永靖县| 汉中市| 房产| 奇台县| 绵阳市| 颍上县| 手游| 盈江县| 小金县| 永嘉县| 隆昌县| 启东市|