The router has two modes: one is hash
mode, the other is history
mode, when using vue -cli
and vue-router
vue
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
vue-router
Default 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
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
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: [...] })
, # symbols mixed in
url
does look a bit inelegantIf you want
It looks better, then use the history
modeBut: in the
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
mode, the front-end URL
must be the same as the actual one. The URL
initiated by the client must be consistent , such as:
, if the backend lacks the correct /fontend /id
route processing, then it will return 404
error If you want to support
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
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
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
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
service code can be named server.js
, Also install express
and connect-history-api-fallback
middlewareStart the back-end service execution command
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false;">const express = require(&#39;express&#39;);
const history = require(&#39;connect-history-api-fallback&#39;)
const app = express()
app.use(history())
app.use(express.static(__dirname+&#39;/static&#39;));
app.listen(5005,(err)=> {
if(!err)consle.log(&#39;服务器启动成功了&#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
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
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,url
Although 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
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!