Table of Contents
Hash mode (hash )
To solve this problem: If you are using
The two modes in
Front-end routing has two modes, one is
Home Web Front-end Vue.js Let's talk about Vue's two routing modes (hash and history)

Let's talk about Vue's two routing modes (hash and history)

Apr 12, 2023 pm 05:50 PM
vue javascript front end

Let's talk about Vue's two routing modes (hash and history)

The router has two modes: one is hash mode, the other is history mode, when using vue -cli and vue-routervue projects built by default, if no special configuration is made, the default is hash mode

Each of these two modes has its own advantages, but the difference in their use will more or less be asked in interviews

Let’s learn together today

Hash mode (hash )

vue-routerDefault hash mode, uses the hash of url (hash) to simulate a complete The URL, when URL changes, the page will not reload. [Related recommendations: vuejs video tutorial, web front-end development]

As shown below

http://localhost/#home
Copy after login

Features: ## The parameters after ## will not be sent to the server. It has good compatibility and will not be sent to the server as part of the path. That is, it will not be included in the HTTP request body. It has no impact at all. It’s just that when our front-end students play by themselves

When the page is refreshed, it will stay on the current page and will not reload

If you think the

hash path is ugly, no Simple, we can use the history mode of routing, which makes full use of history.pushState API or replaceState to complete, url Jump without reloading the page

History mode

historyMode: Add mode mode to the instantiated configuration object, with a value of history That’s it After transformation, the hash mode will become the history mode

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

There is no problem in using these two methods, if you care about the browser# The appearance of ##url

, # symbols mixed in url does look a bit inelegantIf you want

url

It looks better, then use the history modeBut: in the

hash

mode, the content before using the hash symbol will be included In the request body, the number after # will not be sent to the server . However, in

history

mode, the front-end URL must be the same as the actual one. The URL initiated by the client must be consistent , such as:

https://itclan.cn/fontend/id

, if the backend lacks the correct /fontend /id route processing, then it will return 404 error If you want to support

history

mode, then you need back-end support. If you want To completely solve the 404 problem, you need to negotiate with the back-end classmates, because the back-end and front-end routes need to be matched Add a candidate resource on the server to cover all situations, if

url

No static resources can be matched, and a home page should be returned.If 404 appears, it is easy for users to think it is a bug

How to solve the 404 problem of refreshing the page in the foreground

To solve this problem: If you are using

Node

for back-end service, then you can add a middleware to the Node background such as: connect-history -api-fallback can solve this 404 problemIf it is

java

or php, find a back-end classmate and let the back-end routing and front-end routing Do matching, or use Ngnix as an intermediate proxyThe following simple

Node

service code can be named server.js, Also install express and connect-history-api-fallbackmiddlewareStart the back-end service execution command

node server.js

<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>const express = require(&amp;#39;express&amp;#39;); const history = require(&amp;#39;connect-history-api-fallback&amp;#39;) const app = express() app.use(history()) app.use(express.static(__dirname+&amp;#39;/static&amp;#39;)); app.listen(5005,(err)=&gt; { if(!err)consle.log(&amp;#39;服务器启动成功了&amp;#39;) })</pre><div class="contentsignin">Copy after login</div></div>Put the code in the dist<p> file generated by the front-end packaging into <code>static. Through this operation, you can solve the problem of refreshing the page, 404 QuestionUnderstanding the application of single-page spa

The two modes in

vue-router

used by our front-end are single-page applications, and there is only one in the entire application router Router can be obtained through the $router attribute. In other words, the entire application has only one complete page. At the same time, click on the navigation link in the page. The page will not be refreshed, it will only be a partial update of the page

And the data in our page often needs to be obtained through an

ajax

requestNew projects currently being developed, Both front-end and back-end are separated, and they are basically single-page applications.

Summary

Front-end routing has two modes, one is

hash

mode, and the other is history mode, where hash mode is the default mode, # will not be sent to the server, and the loading page will not be refreshed, while history Mode,urlAlthough it looks better, if you want complete support, you need support from back-end classmates. The back-end routing and the front-end routing need to match Otherwise, they will be deployed online , as soon as the page is refreshed, the problem

404

will appear<p> (Learning video sharing: <a href="https://www.php.cn/course/list/18.html" target="_blank">vuejs introductory tutorial</a>, <a href="https://www.php.cn/course/list/91.html" target="_blank" textvalue="编程基础视频">Basic programming video</a>)</p>

The above is the detailed content of Let's talk about Vue's two routing modes (hash and history). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

How to reference js file with vue.js How to reference js file with vue.js Apr 07, 2025 pm 11:27 PM

There are three ways to refer to JS files in Vue.js: directly specify the path using the &lt;script&gt; tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

How to use watch in vue How to use watch in vue Apr 07, 2025 pm 11:36 PM

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

What does vue multi-page development mean? What does vue multi-page development mean? Apr 07, 2025 pm 11:57 PM

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

How to return to previous page by vue How to return to previous page by vue Apr 07, 2025 pm 11:30 PM

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses &lt;router-link to=&quot;/&quot; component window.history.back(), and the method selection depends on the scene.

How to query the version of vue How to query the version of vue Apr 07, 2025 pm 11:24 PM

You can query the Vue version by using Vue Devtools to view the Vue tab in the browser's console. Use npm to run the "npm list -g vue" command. Find the Vue item in the "dependencies" object of the package.json file. For Vue CLI projects, run the "vue --version" command. Check the version information in the &lt;script&gt; tag in the HTML file that refers to the Vue file.

How to use function intercept vue How to use function intercept vue Apr 08, 2025 am 06:51 AM

Function interception in Vue is a technique used to limit the number of times a function is called within a specified time period and prevent performance problems. The implementation method is: import the lodash library: import { debounce } from 'lodash'; Use the debounce function to create an intercept function: const debouncedFunction = debounce(() =&gt; { / Logical / }, 500); Call the intercept function, and the control function is called at most once in 500 milliseconds.

See all articles