- Vue.js 2 Web Development Projects
- Guillaume Chau
- 460字
- 2021-07-02 22:34:33
Child-to-parent communication with custom events
Previously, we used props to communicate from a parent component to its children. Now, we would like to do the opposite and communicate from one child component to its parent. For our card component, we would like to tell the parent component that the card is being played by the player when they click on it. We can't use props here, but we can use custom events. In our components, we can emit events that can be caught by the parent component with the $emit special method. It takes one mandatory argument, which is the event type:
this.$emit('play')
We can listen to the custom events inside the same Vue instance with the $on special method:
this.$on('play', () => {
console.log('Caught a play event!')
})
The $emit method also sends a 'play' event to the parent component. We can listen to it in the parent component template with the v-on directive just like we did before:
<card v-on:play="handlePlay" />
You can also use the v-bind shorthand:
<card @play="handlePlay" />
We can also add as many arguments as we like that will get passed to the handler methods:
this.$emit('play', 'orange', 42)
Here, we emitted a 'play' event with the following two arguments-- 'orange' and 42.
In the handle, we can get them via the arguments, as follows:
handlePlay (color, number) {
console.log('handle play event', 'color=', color, 'number=', number)
}
The color argument will have the 'orange' value and the number argument will have the 42 value.
Like we saw in the preceding section, custom events are completely separate from the browser event system. The special methods--$on and $emit--are not aliases to the standard addEventListener and dispatchEvent. That explains why we need the .native modifier on components to listen to browser events such as 'click'.
Back to our card component, we just need to emit a very simple event to tell the parent component that the card is being played:
- First, add the method that will emit the event:
methods: {
play () {
this.$emit('play')
},
},
- We would like to call this method when the user clicks on the card. Just listen to a browser click event on the main card p element:
<p class="card" :class="'type-' + def.type" @click="play">
- We are done with the card component. To test this, listen to the 'play' custom event in the main component template:
<card :def="testCard" @play="handlePlay" />
Now, the handlePlay method will be called whenever the 'play' event is emitted.
We could just have listened to a native click event instead, but it's in most cases a good idea to use custom events to communicate between components. For example, we could also emit the 'play' event when the user uses another method, such as using the keyboard to select the card and pressing Enter; we won't implement that method in this book though.
- HTML5+CSS3+JavaScript從入門到精通:上冊(cè)(微課精編版·第2版)
- VMware View Security Essentials
- Java高并發(fā)核心編程(卷2):多線程、鎖、JMM、JUC、高并發(fā)設(shè)計(jì)模式
- Vue.js入門與商城開(kāi)發(fā)實(shí)戰(zhàn)
- 跟小海龜學(xué)Python
- PostgreSQL技術(shù)內(nèi)幕:事務(wù)處理深度探索
- 算法大爆炸:面試通關(guān)步步為營(yíng)
- Learning AWS Lumberyard Game Development
- DevOps Automation Cookbook
- Mastering Ubuntu Server
- Python高效開(kāi)發(fā)實(shí)戰(zhàn):Django、Tornado、Flask、Twisted(第2版)
- 人人都是網(wǎng)站分析師:從分析師的視角理解網(wǎng)站和解讀數(shù)據(jù)
- Learning OpenCV 3 Computer Vision with Python(Second Edition)
- Java Web開(kāi)發(fā)詳解
- 新一代SDN:VMware NSX 網(wǎng)絡(luò)原理與實(shí)踐