I have some pages (in Laravel) that each includes a form with different action URI's:
<form method="post" action="/route1">
...
<button @click="emitEvent('event1')">
</form>
In Vuejs root I have only a method that calls the event globally:
const app = new Vue({
el: '#app',
methods: {
emitEvent(event, payload) {
EventBus.$emit(event, payload);
}
}
});
In my Component I have a listener for the global event:
data() {
return {
someKey: false //this value is dynamic
}
}
mounted() {
EventBus.$on('event1', (event) => {
console.log(event); //returns undefined
if(this.someKey) {
window.location = "/another-url";
//Go to this url ONLY if the is true, else go to the url from the action attibute of the form
}
});
}
So basically, if a certain condition is meet in vue, I want to load that page.
Right now, the page will load the url from html form even if the condition in vuejs is meet or not.
I tried @click.prevent="emitEvent('event1')" instead of @click but in this way, if the condition is false and the window.location doesn't run, it gets stuck.
I am thinking to access the event object and then manually preventDefault() but only if necesary, so if that's not the case, the page will load the url from the form's action, but I can't find a way to get that event object inside Vue.
I found that the event name is in EventBus._events.event1 but I can't go forward because there are only empty properties and I`m not sure haw can I access the default event so I can prevent it.
I`m sure there it must be a easier/shorter and better way then giving an id/class to each form, then access it, then get its action attribute which is the url I need and finally if access that url if the condition is not meet. This way it may work, but it's an old style maybe?
Anyway, I am stack here and I appreciate any help.