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-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
Hash mode (hash )
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
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
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:php;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
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,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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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.

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

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.

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.

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

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 <script> tag in the HTML file that refers to the Vue file.

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(() => { / Logical / }, 500); Call the intercept function, and the control function is called at most once in 500 milliseconds.
