Home  >  Article  >  Web Front-end  >  Let’s talk about Vue’s two routing modes (hash and history)

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

青灯夜游
青灯夜游forward
2023-04-12 17:50:301912browse

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

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: [...]
})

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

<pre class="brush:js;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>Put the code in the

dist

file generated by the front-end packaging into 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:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete