- 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.
推薦閱讀
- Manga Studio Ex 5 Cookbook
- Python自動化運維快速入門
- Java FX應用開發教程
- 人人都是網站分析師:從分析師的視角理解網站和解讀數據
- Jupyter數據科學實戰
- UML 基礎與 Rose 建模案例(第3版)
- Spring快速入門
- Haskell Data Analysis Cookbook
- Swift語言實戰晉級
- 愛上C語言:C KISS
- Learning Jakarta Struts 1.2: a concise and practical tutorial
- Python Machine Learning Cookbook
- Mastering XenApp?
- Spring MVC Blueprints
- Instant AppFog