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

MySQL 5.7.17 installation and configuration graphic tutorial

Features of MySQL: MySQL is a relational database...

How to use Vue to develop public account web pages

Table of contents Project Background start Create...

MySQL variable principles and application examples

In the MySQL documentation, MySQL variables can b...

Basic usage and pitfalls of JavaScript array sort() method

Preface In daily code development, there are many...

A brief discussion on Axios's solution to remove duplicate requests

Table of contents 1. Cancel duplicate requests 2....

Teach you how to use webpack to package and compile TypeScript code

TypeScript Bundling webpack integration Usually, ...

Execute initialization sql when docker mysql starts

1. Pull the Mysql image docker pull mysql:5.7 2. ...

Disadvantages and reasonable use of MySQL database index

Table of contents Proper use of indexes 1. Disadv...

How to use nginx to build a static resource server

Taking Windows as an example, Linux is actually t...

MySql learning day03: connection and query details between data tables

Primary Key: Keyword: primary key Features: canno...

The webpage cannot be opened because the div element lacks a closing tag

At first I thought it was a speed issue, so I late...

JavaScript dynamically generates a table with row deletion function

This article example shares the specific code of ...

An article to solve the echarts map carousel highlight

Table of contents Preface toDoList just do it Pre...

Simple usage examples of MySQL custom functions

This article uses examples to illustrate the usag...