- Vue.js 2 Web Development Projects
- Guillaume Chau
- 246字
- 2021-07-02 22:34:34
Playing a card
Now, we need to handle the 'play' event in the hand component we emit in the cards when the user clicks on them, and emit a new 'card-play' event to the main component with an additional argument--the played card in question.
- First, create a new method called handlePlay. It takes a card argument and emits the new event to the parent component:
methods: {
handlePlay (card) {
this.$emit('card-play', card)
},
},
- Then, add a listener to our cards for the 'play' event:
<card v-for="card of cards" :def="card.def"
@play="handlePlay(card) />
As you can see here, we directly use the iterator variable card of the v-for loop. That way, we don't need the card component to emit its card item since we already know what it is.
To test the card play, we will only remove it from the hand for now.
- Create a new temporary method called testPlayCard in the main component in the main.js file:
methods: {
// ...
testPlayCard (card) {
// Remove the card from player hand
const index = this.testHand.indexOf(card)
this.testHand.splice(index, 1)
}
},
- Add the event listener for the 'card-play' event on the hand component in the main template:
<hand v-if="!activeOverlay" :cards="testHand" @card-play="testPlayCard" />
If you click on a card, it should now emit a 'play' event to the hand component, which will then emit a 'card-play' event to the main component. It will, in turn, remove the card from the hand, making it disappear. To help you debug this sort of use case, the devtools have an Events tab:

- Go語言高效編程:原理、可觀測性與優化
- FFmpeg入門詳解:音視頻流媒體播放器原理及應用
- 概率成形編碼調制技術理論及應用
- 大模型RAG實戰:RAG原理、應用與系統構建
- C程序設計實踐教程
- Android系統原理及開發要點詳解
- Microsoft 365 Certified Fundamentals MS-900 Exam Guide
- SQL Server 2016 從入門到實戰(視頻教學版)
- Python函數式編程(第2版)
- VMware vSphere Design Essentials
- Web程序設計與架構
- Mastering VMware vSphere Storage
- Flask Web開發實戰:入門、進階與原理解析
- SQL Server 2012數據庫管理與開發(慕課版)
- Web前端開發全程實戰:HTML5+CSS3+JavaScript+jQuery+Bootstrap