I have create a new Vue instance and assigned it to my window, which should give me access to it throughout all components. This should give me access to events emitted anywhere within my application.
However for some reason this is not the case, can someone advise me?
app.js
window.Vue = require('vue');
window.events = new Vue();
window.app = new Vue({
el: '#app',
methods: {
emit: function (name, params) {
window.events.$emit(name, params);
}
}
});
I have created a emit
method within my app for my components to use, as simply calling v-on:change="window.events.$emit('makeChanged', $event)"
was not working, I was getting the error that window
is not defined.
I'm listening for any emitted events from the emit
within my component as follows:
window.events.$on('makeChanged', function(evt) {});
Edit:
Still can't get this working...
app.js
Vue.component('models-select', require('./components/results-text/ModelsSelect.vue'));
window.app = new Vue({
el: '#app',
methods: {
emit(name, ...params) {
this.$root.$emit(name, ...params);
},
...
ModelsSelect.vue
export default {
props: [ 'endpoint', 'makeId', 'modelId', 'typeId'],
mounted() {
this.$root.$on('make-changed', function(evt) {
console.log(evt);
});
Within one of my views (add.blade.php)
<select name="make" id="makes" v-on:change="emit('make-changed', $event)" tabindex="2" class="{{ old('make') ? ' is-changed' : '' }}">