Skipping beforeRouteLeave in VueJS

In my Vue SPA, there is a component where I am using the beforeRouteLeave guard to prevent accidental page exits. However, within this component, I occasionally make an ajax request that, upon successful completion, needs to redirect the user to another location.

The issue arises when the ajax request succeeds and $router.push() tries to redirect the page but beforeRouteLeave prevents it from doing so.

Is there a way to temporarily override beforeRouteLeave and allow $router to perform the redirection?

<script>

methods:{
    someAjaxCall()
    {
        axios.post('...')
        .then(res=>{
            this.$router.push({
                name: 'route.name.here'
            })
        })
    }
},

beforeRouteLeave(to, from, next){
    next(window.confirm('Are you sure? Progress will be discarded.'))
},

</script>

Answer №1

To achieve bypassing the guard, I can utilize a boolean variable with a condition:

<script>

data(){
    return {
        preventRouteLeave: false,
    }
},
methods:{
    makeAjaxCall()
    {
        axios.post('...')
        .then(res=>{
            this.preventRouteLeave = true
            this.$router.push({
                name: 'route.name.here'
            })
        })
    }
},

beforeRouteLeave(to, from, next){
    if(this.preventRouteLeave)
        next()
    else
        next(window.confirm('Are you sure? Progress will be discarded.'))
},

</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Refine your search with a JSON object description in expressJS and UnderscoreJS

[ { "id": 1, "description": "Empty the garbage bin", "completed": false }, { "id": 2, "description": "Dine out for dinner", "completed": false }, { "id": 3, "description": "Exercise at the fitness center", "com ...

Issue with Angular: PDF rendering delayed until window resize

Recently, I encountered an issue with rendering a PDF in Chrome while using an AJAX call with Angular. Strangely, the PDF would only show up in the browser if I resized the window or opened the console. Surprisingly, everything worked fine in Firefox. Jav ...

Laravel encounters an issue when trying to access the user ID

I am currently working on a project using Laravel and Vue. I'm trying to pass the user ID to the table when the user is authenticated, but I'm facing an issue where the authenticated user's ID is not being passed to the query, resulting in t ...

Is there a way to retrieve all IDs within an array of objects using a foreach loop and merge the data associated with each ID?

I am currently working on a TypeScript API where I have merged all the data from this API with some additional data obtained from another API function. I have provided a snippet of my code which adds data to the first index of an array. M ...

Dynamic user group system that leverages Ajax technology for seamless integration with an HTML interface, powered by PHP and

I am new to this, so please forgive my lack of knowledge. Currently, I am in the process of creating an Ajax-driven web application for managing user contact groups. This application allows users to store contacts based on assigned groups. Once a user con ...

Issue with the Ajax auto-complete feature in Internet Explorer

I am facing an issue with my "ajax suggestion list" getting hidden behind the "select menu" located right below the text box that triggers the ajax function. This problem only occurs in IE 6.0, while it works fine in other browsers. I have already disabled ...

Creating JavaScript classes based on JSON schemas

I have a JSON file containing information and data related to my work, presented in the following format: { "Employees":[ { "id": 1, "fullName": "Test Test" } ], "Infos":[ { "id": 1, ...

The router is displaying the component directly on the current page rather than opening it on a separate page

The issue is that the router is rendering the page on the same page instead of generating a new one. When clicking on the Polls link, it only renders the page there but the URL changes in the browser. Polls.js import React from 'react'; import ...

Obtaining the contents of a local directory with JavaScript

Currently, I am attempting to access the contents of a local folder using JavaScript. Despite my efforts with the Filesystem API, an error is consistently appearing: DOMException: It was determined that certain files are unsafe for access within a Web app ...

The issue with HTML where the header and footer elements are continuously repeating when

I'm struggling with the title header and footer repeating on every printed page. I just want to show the title at the top of the page and display the footer at the end of the print page. Can anyone provide a solution or suggestion? You can view my fid ...

Synchronizing data between two Vue components when deleting an item

Feeling uncertain about the approach I am taking with this data. Essentially, I have the following dataset: surrounding: { data: [ { id: 1, name: 'test' }, { id: 2, name: 'test' }, { id: 3, name: 'test& ...

The HTML element <a> can have both a href attribute and a onclick attribute

I'm currently facing a challenge with my project - I am trying to create a submenu that includes all sub-pages within a single HTML file. However, whenever I attempt to use both href and onclick, the functionality fails to work as intended. The only ...

The Vue ChartJS fails to display properly after the page is refreshed

Although this issue may appear to be a common one, some of the suggested solutions are outdated or no longer functional. My objective is to ensure that the chart remains responsive even after the page reloads. I attempted to implement the solution provided ...

Express server experiencing issues with generating Swagger documentation

I've been working on an Express API and decided to implement documentation using Swagger and JSDoc. However, the documentation is not working as expected. Here's how I've set it up: docs.ts: import swaggerJSDoc, { Options } from "swagg ...

The typescript error TS2339 is triggered by the property 'webkitURL' not being found on the 'Window' type

Currently utilizing Angular 2 in a project that is compiled with TypeScript. Encountering an issue when attempting to generate a blob image: Error TS2339: Property 'webkitURL' does not exist on type 'Window' The TypeScript code causi ...

Exploring a utility function for saving object information in a dynamic state

Imagine my state was structured like this: state = { customer: { name: { elementType: "input", elementConfig: { type: "text", placeholder: "Your Name" }, value: "" }, street: { e ...

Displaying the countdown of days and hours until a specific date is just a matter of using

Currently, I am tackling a project that necessitates a specific text response related to a date object. "1 day 7 hours away" --- This format is crucial; alternatives such as "31 hours away" or "1 day away" will not suffice. -- For language switching purpo ...

Angular Resolve Upon Application Reloading

Is there a way to postpone the initialization of my Application Controller (similar to using the 'resolve' attribute in the router) when reloading a page? Or more importantly, how can I delay the rendering of the view until my controller has succ ...

Checkbox search column in jQuery DataTables

I have implemented jQuery DataTables Individual Column Searching on a table where one of the columns contains checkboxes. HTML Structure <table id="NewTable" class="table table-bordered table-striped"> <thead> <tr> ...

Executing a node module that is installed globally from within an electron application

I have encountered an issue while attempting to launch vue-devtools from within my application. The error message I am receiving is: Uncaught Exception: Error: spawn vue-devtools ENOENT     at Process.ChildProcess._handle.onexit &n ...