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

Solution to mysql prompt "got timeout reading communication packets"

Error message: user: 'root' host: `localh...

WeChat applet implements video player sending bullet screen

This article shares the specific code for WeChat ...

Implementation of Nginx operation response header information

Prerequisite: You need to compile the ngx_http_he...

Website construction experience summary

<br />What principles should be followed to ...

How to completely uninstall Docker Toolbox

Docker Toolbox is a solution for installing Docke...

How to use VirtualBox to build a local virtual machine environment on Mac

1. Big Data and Hadoop To study and learn about b...

Rhit efficient visualization Nginx log viewing tool

Table of contents Introduction Install Display Fi...

Detailed explanation of the solution to Tomcat's 404 error

The 404 problem occurs in the Tomcat test. The pr...

Let's talk about MySQL joint query in detail

Table of contents Union query 1. Query the ID and...

How to use the Linux seq command

1. Command Introduction The seq (Sequence) comman...

Implementing a simple age calculator based on HTML+JS

Table of contents Preface Demonstration effect HT...

nginx proxy_cache batch cache clearing script introduction

Preface: I used the official nginx proxy_cache as...