In resources/assets/js/app.js I have created eventHub
Vue instance:
require('./bootstrap');
var eventHub = new Vue();
Vue.component('todos-list', require('./components/todos/TodoList.vue'));
Vue.component('todos-add', require('./components/todos/TodoAdd.vue'));
const app = new Vue({
el: '#app'
});
How can I use it in components that are created in separate .vue files?
For example, I also have two components:
todos-list located in /components/todos/TodoList.vue, which is used to fetch the data from server-side using vue-resource:
<template id="todos-list-template"> <div> <ul v-if="todos.length > 0"> <li v-for="todo in todos"> {{ todo.title }} </li> </ul> <p v-else>You don't hanve any Todos.</p> </div> </template> <script> export default { template: '#todos-list-template', data() { return { todos: {} } }, mounted() { this.$http.get('api/vue/todos').then(function(response) { this.todos = response.data; }); }, methods: { handleAddedTodo: function(new_todo) { this.todos.push(new_todo); } }, created: function() { eventHub.$on('add', this.handleAddedTodo); }, destroyed: function() { eventHub.$off('add', this.handleAddedTodo); } } </script>
todos-add located in /components/todos/TodoAdd.vue which is used to add (save) the new 'todo' using vue-resource:
<template id="todos-add-template"> <div> <form v-on:submit.prevent="addNewTodo()"> <div class="form-group"> <input v-model="newTodo.title" class="form-control" placeholder="Add a new Todo"> </div> <div class="form-group"> <button>Add Todo</button> </div> </form> </div> </template> <script> export default { template: '#todos-add-template', data() { return { newTodo: { id: null, title: this.title, completed: false } } }, methods: { addNewTodo() { this.$http.post('api/vue/todos', { title: this.newTodo.title }).then(function(response) { if (response.status == 201) { eventHub.$emit('add', response.data); this.newTodo = { id: null, title: this.title, completed: false } } }, function(response) { console.log(response.status + ' - '+ response.statusText); }) } } } </script>
When I add (save) new 'todo' using the todos-add component (TodoAdd.vue) - I want to update data in todos-list component. If I understood well the documentation - for component communication we need to use centralized event hub. And that's what I tried - but I am getting the following error:
eventHub is not defined
I guess because it is defined in app.js, but how can I use in components that are created in separate .vue files?