search
HomeWeb Front-endVue.jsA brief analysis of the difference between hash and history in Vue front-end routing

VueWhat is the difference between front-end routing hash and history? In this article, we will learn about the difference between front-end routing hash and history. I hope it will be helpful to everyone!

A brief analysis of the difference between hash and history in Vue front-end routing

Before you understand these two routes, whether it is vue or react, it will inevitably be done when the project is created. When choosing between routes, you will inevitably get entangled between hash and history , or you may just use the default hash with # in a confused way. Routing, after reading this sharing, I guarantee that you will not have trouble choosing which route to choose, and you can choose according to your needs. If you have any questions, please point them out in the comment area and let’s communicate together.

A brief introduction to Vue Router

  • Vue Router is the official routing plug-in of Vue.js , it is deeply integrated with Vue.js and is suitable for building single-page applications. vue's single-page application is based on routing and components. Routing is used to set access paths and map paths and components. Traditional page applications use some hyperlinks to achieve page switching and jumps. In the vue-router single-page application, it is the switching between paths, that is, the switching of components. The essence of the routing module is to establish the mapping relationship between url and page. (Learning video sharing: vue video tutorial)
  • As for why we can’t use the a tag, this is because everything we use Vue to do is a single-page application, which is equivalent to having only one mainindex.html page, so the tag you wrote will not work, you must use vue-router for management.

Vue Router implementation principle

  • Before understanding the routing mode, we must first understand,vue-roter What is the implementation principle, what is a single page application, and what are its characteristics? This will make it easier to deepen your understanding of routing.

  • SPA Single page and application method: A single page application has only one complete page; when it loads the page for the first time, it will html The page is downloaded together with all other page components, so that when it switches pages, it will not load the entire page, but only update the content in a specified container.

  • One of the cores of single page application (SPA) is: updating the view without re-requesting the page.

  • The three major steps of the underlying implementation of the router object are (1)Monitor address bar changes; (2)Find the page corresponding to the current path Component; (3) Replace the found page component with to the location of router-view.

  • vue-router When implementing single-page front-end routing, two methods are provided: Hash mode and History Mode; vue2 determines which method to use based on the mode parameter, while vue3 uses the history parameter. Below we will learn more about this attribute.

    A brief analysis of the difference between hash and history in Vue front-end routing

##Hash

Brief description

A brief analysis of the difference between hash and history in Vue front-end routing

  • vue-router Default hash mode - Use URL hash to simulate a complete URL, so when the URL changes, the page will not be reloaded. hash (#) is the anchor point of URL, which represents a position in the web page. If you only change the part after #, the browser will only scroll to At the corresponding position, the web page will not be reloaded, which means that # is used to guide the browser's actions and is completely useless to the server. HTTP will not be included in the request. ##, and every time you change the part after #, a record will be added to the browser's access history. Use the "Back" button to return to the previous position, sohash The mode changes the anchor point value and renders different data at the specified DOM position according to different values.

  • # The symbol itself and the characters following it are called hash, which can be accessed through window.location.hash Property reading.

Features

  • hash Although it appears in the URL, it will not be included in HTTP Requesting. It is used to guide browser actions and is completely useless on the server side. Therefore, changing hash will not reload the page
  • You can add a listener for the change of hash Event:
  •    window.addEventListener("hashchange", fncEvent, false)
  • Every time the hash (window.location.hash) is changed, a record will be added to the browser’s access history
  • The url contains a # number.

Settings

  • vue3 Settings hash Pattern routing

    A brief analysis of the difference between hash and history in Vue front-end routing

history

##Brief description

A brief analysis of the difference between hash and history in Vue front-end routing

    ##history
  • It is another mode of routing. Since the hash mode will contain # in the URL, if we do not want to have #, we can use the history mode of routing, and only need to add router## in the response. # When configuring rules, just add it. vue’s routing defaults to hash mode. Uses the new pushState()
  • and
  • replaceState() methods in HTML5 History Interface. These two methods are applied to the browser's history stack. Based on the current back, forward, go
  • , they provide the function of modifying the history record. . It's just that when they perform modifications, although the current URL has been changed, the browser does not immediately send a request to the backend.
  • Settings

A brief analysis of the difference between hash and history in Vue front-end routingFeatures

Route jump No need to reload the page.

    Without #, most people think it looks much better than hash routing.
  • The compatibility is not as good as hash, which will be explained below
  • Production environment problems and solutions

When we put history After the project is deployed to the server, we enter a URL in the browser (for example,

www.test.com
    ). At this time, it will go through dns resolution, and after getting the ip address, we will send it to the server based on the ip address. Initiate a request, and after the server receives the request, it returns the corresponding results (html, css, js). If we set a redirect on the front end, the page will jump to
  • www.test.com/home, and the front end will match the corresponding component and render it on the page. If we refresh the page at this time, the browser will send a new request www.test.com/home. If the backend server does not have an interface corresponding to /home, then 404 will be returned.

    A brief analysis of the difference between hash and history in Vue front-end routingThe solution to the production environment refresh 404 can be done by proxy forwarding in

    nginx
  • , and configured in nginx to check whether the resources in the parameters exist in order. If None were found, so nginx internally redirected to the project homepage.
  • A brief analysis of the difference between hash and history in Vue front-end routing

  • Development environment - historyApiFallback

Some friends may have questions about why the development environment I haven't encountered this problem. Isn't it the same refresh operation as in production?
  • A brief analysis of the difference between hash and history in Vue front-end routing

    I also had questions here. After checking the relevant information, I found that
  • webpack# in
  • vue-cli

    ## Helped us handle it

    A brief analysis of the difference between hash and history in Vue front-end routingIf we change this configuration to false, the browser will regard our request as a get request. If If the backend does not have a corresponding interface, the following error message will appear.

  • A brief analysis of the difference between hash and history in Vue front-end routing

    1A brief analysis of the difference between hash and history in Vue front-end routing##Summary

That’s it We use the two routing modes and differentiation of vue-roter

. To put it simply,
    hash
  • is good for routing compatibility, but it looks ugly with #,

    history is the same as the normal url path, but needs to be configured separately on the server. Everyone can use it as needed according to their own preferences. Students who have questions are welcome to communicate in the comment area.

(Learning video sharing: web front-end development, Basic programming video)

The above is the detailed content of A brief analysis of the difference between hash and history in Vue front-end routing. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
Vue.js vs. React: Which Framework is Right for You?Vue.js vs. React: Which Framework is Right for You?May 01, 2025 am 12:21 AM

Vue.js is suitable for fast development and small projects, while React is more suitable for large and complex projects. 1.Vue.js is simple and easy to learn, suitable for rapid development and small projects. 2.React is powerful and suitable for large and complex projects. 3. The progressive features of Vue.js are suitable for gradually introducing functions. 4. React's componentized and virtual DOM performs well when dealing with complex UI and data-intensive applications.

Vue.js vs. React: A Comparative Analysis of JavaScript FrameworksVue.js vs. React: A Comparative Analysis of JavaScript FrameworksApr 30, 2025 am 12:10 AM

Vue.js and React each have their own advantages and disadvantages. When choosing, you need to comprehensively consider team skills, project size and performance requirements. 1) Vue.js is suitable for fast development and small projects, with a low learning curve, but deep nested objects can cause performance problems. 2) React is suitable for large and complex applications, with a rich ecosystem, but frequent updates may lead to performance bottlenecks.

Vue.js vs. React: Use Cases and ApplicationsVue.js vs. React: Use Cases and ApplicationsApr 29, 2025 am 12:36 AM

Vue.js is suitable for small to medium-sized projects, while React is suitable for large projects and complex application scenarios. 1) Vue.js is easy to use and is suitable for rapid prototyping and small applications. 2) React has more advantages in handling complex state management and performance optimization, and is suitable for large projects.

Vue.js vs. React: Comparing Performance and EfficiencyVue.js vs. React: Comparing Performance and EfficiencyApr 28, 2025 am 12:12 AM

Vue.js and React each have their own advantages: Vue.js is suitable for small applications and rapid development, while React is suitable for large applications and complex state management. 1.Vue.js realizes automatic update through a responsive system, suitable for small applications. 2.React uses virtual DOM and diff algorithms, which are suitable for large and complex applications. When selecting a framework, you need to consider project requirements and team technology stack.

Vue.js vs. React: Community, Ecosystem, and SupportVue.js vs. React: Community, Ecosystem, and SupportApr 27, 2025 am 12:24 AM

Vue.js and React each have their own advantages, and the choice should be based on project requirements and team technology stack. 1. Vue.js is community-friendly, providing rich learning resources, and the ecosystem includes official tools such as VueRouter, which are supported by the official team and the community. 2. The React community is biased towards enterprise applications, with a strong ecosystem, and supports provided by Facebook and its community, and has frequent updates.

React and Netflix: Exploring the RelationshipReact and Netflix: Exploring the RelationshipApr 26, 2025 am 12:11 AM

Netflix uses React to enhance user experience. 1) React's componentized features help Netflix split complex UI into manageable modules. 2) Virtual DOM optimizes UI updates and improves performance. 3) Combining Redux and GraphQL, Netflix efficiently manages application status and data flow.

Vue.js vs. Backend Frameworks: Clarifying the DistinctionVue.js vs. Backend Frameworks: Clarifying the DistinctionApr 25, 2025 am 12:05 AM

Vue.js is a front-end framework, and the back-end framework is used to handle server-side logic. 1) Vue.js focuses on building user interfaces and simplifies development through componentized and responsive data binding. 2) Back-end frameworks such as Express and Django handle HTTP requests, database operations and business logic, and run on the server.

Vue.js and the Frontend Stack: Understanding the ConnectionsVue.js and the Frontend Stack: Understanding the ConnectionsApr 24, 2025 am 12:19 AM

Vue.js is closely integrated with the front-end technology stack to improve development efficiency and user experience. 1) Construction tools: Integrate with Webpack and Rollup to achieve modular development. 2) State management: Integrate with Vuex to manage complex application status. 3) Routing: Integrate with VueRouter to realize single-page application routing. 4) CSS preprocessor: supports Sass and Less to improve style development efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),