- Vue.js 2 Web Development Projects
- Guillaume Chau
- 138字
- 2021-07-02 22:34:33
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.
推薦閱讀
- Ceph Cookbook
- 深入淺出Java虛擬機(jī):JVM原理與實(shí)戰(zhàn)
- 深入淺出Prometheus:原理、應(yīng)用、源碼與拓展詳解
- Dependency Injection in .NET Core 2.0
- Mastering Python Scripting for System Administrators
- 大學(xué)計(jì)算機(jī)基礎(chǔ)(第2版)(微課版)
- Python完全自學(xué)教程
- 機(jī)器學(xué)習(xí)與R語言實(shí)戰(zhàn)
- SQL 經(jīng)典實(shí)例
- C++語言程序設(shè)計(jì)
- Python期貨量化交易實(shí)戰(zhàn)
- 30天學(xué)通C#項(xiàng)目案例開發(fā)
- 快樂編程:青少年思維訓(xùn)練
- VMware vSphere 5.5 Cookbook
- Python計(jì)算機(jī)視覺與深度學(xué)習(xí)實(shí)戰(zhàn)