The difference between hash mode and history mode in vue-router

The difference between hash mode and history mode in vue-router

vue-router has two modes

  • hash mode
  • History mode

1. Single Page Application

Single Page Application

1. There is only one HTML file, all the contents of the entire website are in this HTML, and it is processed by JS

2. Not only is there no refresh in page interaction, but even page jumps are not refreshed. To achieve single-page application ==> front-end and back-end separation + front-end routing. (update the view without re-requesting the page)

Front-end routing

It is actually very simple to implement. It is to match different URL paths, parse them, load different components, and then dynamically render the regional HTML content.

advantage

Good interactive experience, users do not need to refresh the page, the page displays smoothly, good front-end and back-end work separation mode, reduce server pressure,

shortcoming

Not good for SEO, the initial loading takes a long time

2. Hash mode

Principle: It is the onhashchange event, which can be listened to on the window object

The default mode of vue-router is hash mode

1. Use the URL hash to simulate a complete URL

2. When the URL changes, the page will not reload, that is, a single-page application

2. When the hash after # changes, the browser will not send a request to the server. If the browser does not send a request, the page will not be refreshed, and the hasChange event will be triggered. By monitoring the changes in the hash value, the operation of updating part of the page content can be realized.

window.onhashchange = function(event){
    console.log(event.oldURL, event.newURL);
    let hash = location.hash.slice(1);
    document.body.style.color = hash;
}

For hash mode, a hashHistory object is created. When accessing different routes, two things happen:

1. HashHistory.push() adds the new route to the top of the browser's access history stack.

2. HasHistory.replace() replaces the route at the top of the current stack

3.History mode

With the advent of the history api, front-end routing has evolved. In the previous hashchange, you can only change the URL fragment after #, while the history api gives the front-end complete freedom.

  • The history API can be divided into two parts: switching and modifying

3.1 Switching history status

Including back, forward, go three methods corresponding to the browser's forward, backward, jump operations. For example: (Google) browser only has forward and back, no jump. Well, long press the mouse on the forward and backward buttons, and the history of all current windows will appear, so that you can jump (maybe it is more appropriate to call it jump):

history.go(-2);//Back twicehistory.go(2);//Forward twicehistory.back(); //Backhistory.forward(); //Forward

3.2 Modify the history status

Includes two methods: pushState and replaceState

These two methods receive three parameters: stateObj, title, url

history.pushState({color:'red'}, 'red', 'red'})

window.onpopstate = function(event){
    console.log(event.state)
    if(event.state && event.state.color === 'red'){
        document.body.style.color = 'red';
    }
}

history.back();

history.forward();

step

1. Save the state of the page in the state object through pushstate

2. When the page URL changes back to this URL, you can get the state object through event.state

3. So that the page status can be restored

4. The page status here is the page font color. In fact, the page status of the scroll bar position, reading progress, and component switches can all be stored in the state.

3.3 What is the history mode afraid of?

The difference between hash and history

history mode

1. Through the history API, we get rid of the ugly #, but it also has a problem

2. Don’t be afraid of moving forward or backward, just be afraid of refreshing, f5

——The history mode will modify the URL to be the same as the URL of the normal request backend. If the backend is not configured with the corresponding /user/id routing processing, a 404 error will be returned

——So this implementation requires the support of the server, and all routes need to be redirected to the root page.

In ash mode

1. In the previous hashchange, you can only change the URL segment after #. The new URL set by pushState can be any URL with the same origin as the current URL.

2. The front-end routing modifies the information in #, and the browser does not use it when requesting, so there is no problem. However, under history, you can modify the path freely. When refreshing, if there is no corresponding response or resource in the server, a 404 will be displayed in minutes.

Summarize

This concludes this article about the differences between hash mode and history mode in vue-router. For more information about the differences between vue-router modes, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue-router does not allow navigation to the current location (/path) Error reasons and fixes
  • Use vue3.x+vite+element-ui+vue-router+vuex+axios to build a project
  • A super detailed Vue-Router step-by-step tutorial
  • Detailed explanation of the installation and use of Vue-Router
  • Complete steps to use vue-router in vue3
  • Vue-router routing lazy loading and 3 ways to implement it
  • Detailed explanation of the difference between hash mode and history mode in Vue-router
  • vue-router defines meta information meta operation
  • Initial practice of vue3.0+vue-router+element-plus
  • Detailed explanation of vue-router's navigation hook (navigation guard)
  • Vue-Router installation process and principle detailed

<<:  How to uninstall MySQL 8.0 version under Linux

>>:  MySQL 8.0.15 winx64 installation and configuration method graphic tutorial

Recommend

Summary of Vue3 combined with TypeScript project development practice

Table of contents Overview 1. Compositon API 1. W...

A brief introduction to the command line tool mycli for operating MySQL database

GitHub has all kinds of magic tools. Today I foun...

Implementation of React configuration sub-routing

1. The component First.js has subcomponents: impo...

Vue element implements table adding, deleting and modifying data

This article shares the specific code of vue elem...

MySQL data analysis storage engine example explanation

Table of contents 1. Introduce cases 2. View the ...

The most comprehensive explanation of the locking mechanism in MySQL

Table of contents Preface Global Lock Full databa...

mysql8.0.11 winx64 manual installation and configuration tutorial

First of all, let me talk to you about my daily l...

Vue routing lazy loading details

Table of contents 1. What is lazy loading of rout...

Goodbye Docker: How to Transform to Containerd in 5 Minutes

Docker is a very popular container technology. Th...

Detailed explanation of the problems and solutions caused by floating elements

1. Problem Multiple floating elements cannot expa...

Writing a web calculator using javascript

This article mainly records the effect of using j...

Summary of fragmented knowledge of Docker management

Table of contents 1. Overview 2. Application Exam...

JavaScript Basics Objects

Table of contents 1. Object 1.1 What is an object...

Detailed explanation of the relationship between Linux and GNU systems

Table of contents What is the Linux system that w...