Detailed explanation of the two modes of Router routing in Vue: hash and history

Detailed explanation of the two modes of Router routing in Vue: hash and history

hash mode (default)

Working principle: Monitor the hash value changes of the web page—> onhashchange event, get location.hash

Use a hash of the URL to simulate a full URL, so the page doesn't reload when the URL changes.

It will give the user the feeling that the web page has been redirected, but in fact it has not been redirected.

Mainly used in single page applications (SPA)

//Simulation principle//Monitor page hash value changes window.onhashchange = function(){
	// Get the hash value of the current url const _hash = location.hash
	// Display different content according to different hash values ​​switch(_hash) {
	     case '/#a':
	        document.querySelector('#app').innerHTML = '<h1>I am page 1 content</h1>'
	        break;
	     case '/#b':
	        document.querySelector('#app').innerHTML = '<h1>I am page 2 content</h1>'
	        break;
	     case '/#c':
	        document.querySelector('#app').innerHTML = '<h1>I am page 3 content</h1>'
	        break;
	} 
}

history mode

Working principle: Mainly use history.pushState() API to change the URL without refreshing the page.

There are actually five modes that can change the URL without refreshing the page.

history.pushState()

history.replaceState()

history.go()

history.back() --> Equivalent to history.go(-1)

history.forward() --> Equivalent to history.go(1)

Need backend configuration support. If you enter a URL that does not exist, the backend configuration needs to be used as a "backup configuration". Instead of returning a 404, it returns to the home page.

Enable history mode

const router = new VueRouter({
  mode: 'history',
  routes: [...]
})

The above is the detailed content of the two modes of Router routing in Vue, hash and history. For more information about the Router routing mode in Vue, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of Vue-router nested routing
  • vue.js Router nested routes
  • Detailed explanation of Vue routing router
  • Implementation of nested jump of vue routing view router-view
  • Detailed explanation of VueRouter routing
  • Detailed explanation of Vue router routing

<<:  A brief analysis of MySQL locks and transactions

>>:  Analysis of the Linux input subsystem framework principle

Recommend

Tips for making web table frames

<br />Tips for making web table frames. ----...

Detailed explanation of the production principle of jQuery breathing carousel

This article shares the specific process of the j...

Usage and difference of Js module packaging exports require import

Table of contents 1. Commonjs exports and require...

How to understand JavaScript modularity

Table of contents 1. Browser support 2. export ex...

CentOS7.x uninstall and install MySQL5.7 operation process and encoding format modification method

1. Uninstalling MySQL 5.7 1.1查看yum是否安裝過mysql cd y...

Detailed troubleshooting of docker.service startup errors

Execute the following command to report an error ...

Docker uses the nsenter tool to enter the container

When using Docker containers, it is more convenie...

Example code of the spread operator and its application in JavaScript

The spread operator allows an expression to be ex...

Tutorial on how to modify the root password in MySQL 5.7

Version update, the password field in the origina...

Detailed explanation of MySQL sql99 syntax inner join and non-equivalent join

#Case: Query employee salary levels SELECT salary...

How to set mysql5.7 encoding set to utf8mb4

I recently encountered a problem. The emoticons o...

Timeline implementation method based on ccs3

In web projects we often use the timeline control...