Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
118
rated 0 times [  121] [ 3]  / answers: 1 / hits: 17529  / 7 Years ago, sat, april 22, 2017, 12:00:00

I do have three components.



Vue.js



I don't have any influence on what the Datatable component does cause I have it from npm.



Now I want to send an event from EditButton to my Zonelist.



Zonelist component:



<template>
<datatable :columns=table_columns :data=table_rows filterable paginate v-on:remove=removeItem></datatable>
</template>
<script>
import datatable from 'vuejs-datatable';
import moment from 'moment';
export default {
data() {
return {
table_columns: [
{label: Zone, component: 'ZoneLink'},
{label: Last updated, callback (row) {
let locale = $('html').closest('[lang]').attr('lang') || 'en';
moment.locale(locale);
return moment(row.last_updated).format('D. MMM YYYY');
}},
{label: '', component: 'EditButton'}
],
table_rows: [
{
name: xyz.de,
last_updated: 2017-10-21 17:29:50
}
],
form: {
name: '',
errors: []
}
};
},
components: {
datatable
},
methods: {
removeItem (item) {
this.table_rows.forEach((value, index, array) => {
if (value.name === item) {
Vue.delete(array, index);
}
});
}
}
}
</script>


Now my EditButton component $emit()'s the remove event with a parameter.



But nothing happens. So I think vue is not able to locate the listener.



(I'm using method shorthands from ES6 here)



How could I do this properly without mutating Zonelist's state via this.$parent.$parent from the EditButton?


More From » vue.js

 Answers
6

Non parent-child communication in Vue is typically handled via either an event bus, or a state management system.


In this case, unless your application is more complex, the event bus is probably all you need. Since you are using single file components, you may need to declare the bus on the window, probably in your main script.


window.bus = new Vue()

Then in your EditButton, you can emit the event


bus.$emit('some-event', someData)

And in your ZoneList you can listen for it.


bus.$on('some-event', someData => this.doSomething(someData))

[#58047] Thursday, April 20, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
keyonna

Total Points: 521
Total Questions: 104
Total Answers: 96

Location: Samoa
Member since Tue, May 3, 2022
2 Years ago
keyonna questions
;