Switch up the perspective/component without altering the URL

I have implemented a 404 error page functionality successfully using VueRouter:

const router = new VueRouter({
  routes: [
        // ...
        {
            path: '*',
            component: NotFound,
            name: '404',
            meta: {page_title: 'Vuejs&Material Demo | 404 NOT FOUND'}
        },
    ]
});

// ... encountered not found

this.$router.push({name: '404'});

One issue I encountered is that the URL changes as well, whereas I only want the view to change.

I get frustrated when a simple typo leads to redirection to a http://example.com/404, requiring me to retype the URL or rely on browser autofill, both of which are inconvenient.

I'm hoping for a method or logic that can achieve a result similar to this: .

In essence, I want the view to change without altering the URL.

Is there a specific way in Vue or VueRouter to accomplish this?

Answer №1

One way to handle this is by utilizing the router's history object and calling the updateRoute method. Here's a simple implementation:

const newRoute = this.$router.find({ name: '404' });
this.$router.history.updateRoute(newRoute);

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

Having trouble retrieving the latest value of a scope variable when dealing with multiple partial HTML files controlled by a single Angular controller

In my Angular controller, I have an HTML file that includes two partials. Here is a simplified version: HTML: <!DOCTYPE html> <html> <script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> ...

Iterate through and conduct conditional verification

In my project using AngularJS and HTML, I have created a table to display records. I am looking for a way to iterate through the column values and strike through any value in the column that meets a certain condition. For example, in the demo provided her ...

Getting the http response content of a Slim PHP API with Angular JS: A step-by-step guide

My Slim PHP programmed API sends JSON responses in the following format: $response['some-text'] = 'blabla'; $app->response->setStatus(200); $app->response()->headers->set('Content-Type', 'application/json& ...

disableDefault not functioning properly post-fadeout, subsequent loading, and fade-in of new content with a different URL into

After extensive searching, I still haven't found a solution to my problem. My task involves bringing in HTML pages into a div element. I managed to make the content fade out, load new href content, and then fade in the new content. However, I'm ...

Highlighting active items in a Vuetify drawer list parties

** When I select the title and icon of the v-list-item, it only navigates to the corresponding page. However, when I click on the down arrow, it simply expands the list without navigating to the relevant page. Can someone please explain this behavior? ** ...

In VueJS, v-slot is consistently null and void

Here is the issue at hand. Main view: <template> <div class="main"> <Grid :rows=3 :cols=4> <GridItem :x=1 :y=1 /> <GridItem :x=2 :y=1 /> </Grid> </div> </template> <scrip ...

What sets $emit and $dispatch apart in Vue.js?

Vue 2.0 has deprecated the use of $dispatch and $broadcast. I have noticed that $dispatch is similar to $emit. What are the key differences between them? Can we safely replace $dispatch with $emit during migration? ...

Updating a Vuetify 3 Theme on the Fly

I am looking to dynamically switch themes. I have defined a lightTheme and darkTheme. export default createVuetify({ theme: { defaultTheme: "lightTheme", themes: { lightTheme: { dark: false, colors: { pri ...

Generating a JavaScript object from a string to optimize its compatibility with datatables

This inquiry pertains to the plugin available at: var hidecols = '{"sClass": "Hide", "aTargets": [0]},{"sClass": "asdf", "aTargets": [1]},{"sClass": "qwer", "aTargets": [2]}'; var hidecolsobj = eval('(' + hidecols + ')'); ...

A guide on displaying a string returned from JavascriptExecutor in Java

Currently, I am attempting to retrieve the string output from JavascriptExecutor called within Java for the first time. While I have looked at various posts on SO, none seem to detail how to successfully extract the string into Java. After scouring the in ...

The substitution of store.js with the key obtained from locale.json has been initiated

I am looking for a way to dynamically pass data from the current locale into an array in store.js. The array structure is as follows: skills: [ { id: "1", type: "hard", title: "Technical Skills", ...

How can we incorporate SaxonJS higher-order functions into the Node.js runtime, separate from JS/HTML?

We are currently in the process of transitioning an older C# system that relied on custom functions to enhance XSLT processing. Our plan is to convert it to Node.js/saxon-js. After reviewing the documentation, it appears that while higher order functions ...

Store the output of the function in a variable

Here is a script that was provided to me: <div id="container1"> <script> var script = document.createElement("script"); script.type = "text/javascript"; script.text = 'document.write(xmlDoc.getElementsByTagName("INFO") ...

WordPress: Issue with Dropdown onchange Event Not Triggering

I'm having trouble getting an event to fire when the dropdown value changes. Can anyone help me figure out where I might be going wrong? JavaScript code jQuery(document).ready(function($){ jQuery('#_ccounts select').on('change&apo ...

Next.js Server Error: ReferenceError - 'window' is undefined in the application

I am currently in the process of integrating CleverTap into my Next.js application. I have followed the guidelines provided in the documentation Web SDK Quick Start Guide, however, I encountered the following issue: Server Error ReferenceError: window is ...

How to enhance a class in JavaScript by adding a dynamic property

Within my code, I've defined a class which looks like this: class classA { id: number; name: string; constructor(data){ this.id = data?.id || 0; this.name = data?.name || ''; } } What I aim to do now is add ...

Tips for sending dynamic column and row information to antd table

https://i.sstatic.net/XY9Zt.png The following code does not seem to work for an array containing rows and columns Below is an example using antd: const data = []; for (let i = 0; i < 4; i++) { data.push({ key: i, name: `Edward King ${i}`, ...

Utilizing memcache in conjunction with PHP and Node.js

Can JavaScript objects and PHP associative arrays be shared with memcache? Alternatively, is it necessary to convert the data into a string before sharing them? ...

Ensure that the page is fully loaded before proceeding with the redirection

I have a webpage that currently performs a small task before redirecting to a third-party site. The redirection is initiated by using Response.Redirect(); Now, I am looking to integrate another function using JavaScript this time. To add the JavaScript ...

Guide on sending a JSONP POST request using jQuery and setting the contentType

I'm struggling to figure out how to make a jsonp POST request with the correct content type of 'application/json'. Initially, I was able to send the POST request to the server using the jQuery.ajax method: jQuery.ajax({ type: ...