I want to create a toggle effect where a view is hidden if the user tries to revisit it. This can be useful for showing/hiding modal boxes.
Here's the code I attempted:
/* Root Instance */
const app = new Vue({
router,
watch: {
'$route' (to, from) {
if (to.path == from.path) {
alert();
router.go(-1);
} else {
alert(to.path + " != " + from.path);
}
}
}
}).$mount("#app");
However, due to the lack of hash change, the vue router doesn't seem to have a route to handle, making it difficult to trigger the desired functionality.
I could potentially use addEventListener on elements to compare paths and trigger history.back(), but this solution seems suboptimal. It also wouldn't address other URL redirection events.
Is there a better solution provided by vue router? Are there any existing solutions that can handle all possible scenarios without manual intervention?