The composition api enables the usage of the $attrs property that is inherited in each component to fulfill specific requirements.
If you are familiar with using this.$parent.emit because you know that the child will always be part of the same parent, how can you achieve a similar behavior with $attrs?
For instance, imagine a scenario where a table consists of row components and you want to handle row clicks in the table's parent element.
Table Component:
<template>
<row v-bind="$attrs" ></row>
</template>
Row Component:
<template name="row" :item="row" @click=onClick(row)>
Your Row
</template>
export default {
emits: {
row_clicked: () =>{
return true
}
},
onClick(rowData){
this.$emit('row_clicked',rowData)
}
}
Lastly, within a component that includes your table definition, you can implement a method to manage the click event.
<table
@row_clicked=clicked()
>
</table
By applying the @row_clicked directive to the row component in your table, it will respond when the row emits the specified event.