Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
189
rated 0 times [  193] [ 4]  / answers: 1 / hits: 16874  / 8 Years ago, tue, march 1, 2016, 12:00:00

How can I use $dispatch() or $broadcast() on a radio button? I can't do something like this (because I cannot use v-on:click on a radio button):



HTML



 <div class=radio radio-primary>
<label>
<input type=radio name=intern id=intern value=intern
v-on:click=update v-model=selectedrole/>
Showall
</label>
</div>


JS



Vue.component('searchemployees', {
template: '#searchemployees',
data: function()
{
return {
selectedrole: ''
}
},
methods: {
update: function()
{
this.$dispatch('selectedRole', this.selectedrole)
}
}
});

Vue.component('employees', {
template: '#employees',
props:['list'],
data: function()
{
return {
role: ''
}
},
created() {
this.list = JSON.parse(this.list);
},
events: {
'selectedrole': function(role) {
this.role = role
}
}
});


Because I can't use v-on:click on a radio button. How can I do this? (I need selectedrole in 2 components).



Some help please!


More From » vue.js

 Answers
7

You can broadcast the event whenever selectedrole changes using watch:



Vue.component('searchemployees', {
template: '#searchemployees',
data: function()
{
return {
selectedrole: ''
}
},
watch: {
selectedrole: function(newRole)
{
this.$dispatch('selectedRole', newRole)
}
}
});


Then just remove the click listener, it isn't needed


[#63093] Sunday, February 28, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anniejulietteb

Total Points: 740
Total Questions: 125
Total Answers: 97

Location: Benin
Member since Fri, Mar 24, 2023
1 Year ago
anniejulietteb questions
;