I\'m trying to use the on click directive inside a component but it does not seem to work. When I click the component nothings happens when I should get a \'test clicked\' i
As mentioned by Chris Fritz (Vue.js Core Team Emeriti) in VueCONF US 2019
if we had Kia enter
.nativeand then the root element of the base input changed from an input to a label suddenly this component is broken and it's not obvious and in fact, you might not even catch it right away unless you have a really good test. Instead by avoiding the use of the.nativemodifier which I currently consider an anti-pattern will be removed in Vue 3 you'll be able to explicitly define that the parent might care about which element listeners are added to...
$listeners:So, if you are using Vue 2 a better option to resolve this issue would be to use a fully transparent wrapper logic. For this Vue provides a $listeners property containing an object of listeners being used on the component. For example:
{
focus: function (event) { /* ... */ }
input: function (value) { /* ... */ },
}
and then we just need to add v-on="$listeners" to the test component like:
Test.vue (child component)
click here
Now the Demo: We can also use Test.vue (child component) Important: Always use kebab-case for event names. For more information and demo regading this point please check out this answer: VueJS passing computed value from component to parent. Now, we just need to listen to this emitted custom event in parent component like: App.vue So, basically instead of Demo: Vue 3 is going to make our life much easier in many ways. One of the examples for it is that it will help us to create a simpler transparent wrapper with very less config by just using So, with respect to this question, we will not need to update anything in Vue 3 and your code will still work fine as Demo #1: But for complex components with nested elements where we need to apply attributes and events to main Demo #2: component is a fully transparent wrapper, meaning it can be used exactly like a normal .native modifier.
Vue.component('test', {
template: `
div.child{border:5px dotted orange; padding:20px;}
Using $emit method:
$emit method for this purpose, which helps us to listen to child components events in parent component. For this, we first need to emit a custom event from child component like:v-on:click or the shorthand @click we will simply use v-on:my-event or just @my-event.Vue.component('test', {
template: `
div.child{border:5px dotted orange; padding:20px;}
With Vue 3
Using
v-bind="$attrs":v-bind="$attrs". By using this on child components not only our listener will work directly from the parent but also any other attribute will also work just like it a normal const { createApp } = Vue;
const Test = {
template: `
div.child{border:5px dotted orange; padding:20px;}
instead of the parent label we can simply use v-bind="$attrs"const { createApp } = Vue;
const BaseInput = {
props: ['label', 'value'],
template: `
`
};
const App = {
components: { BaseInput },
setup() {
const search = event => {
console.clear();
console.log("Searching...", event.target.value);
};
return { search };
}
};
createApp(App).mount("#myApp");input{padding:8px;}