当前位置:网站首页>Routing mode: hash and history mode
Routing mode: hash and history mode
2022-07-02 16:25:00 【hyduan200】
hash Pattern url It always has # Number , We use this mode by default in development .
If the user considers url Then you need to use history Pattern , because history Model does not # Number , It's a normal url Suitable for promotion .
Of course, there are differences in their functions , For example, we are developing app There's a sharing page when you're on the Internet , So this shared page is used vue or react
It's done , Let's share this page with a third party app in , yes , we have app Inside url It's not allowed to have # The no. , So will # If you want to get rid of the number, you need to use history Pattern .
But use history Another problem with the model is , When visiting the secondary page , Refresh , There will be 404 error , Then we need to cooperate with the back-end people and let them configure apache or nginx Of url Redirect , Redirecting to your home page route is ok La .
difference
hash | history |
---|---|
url Show that #, very Low | url Show none #, good-looking |
Press enter to refresh It can be loaded to hash Value corresponding page | Enter refresh is generally 404 It fell off |
No backend configuration required | Back end configuration is required |
Support lower version browsers and IE | browser HTML5 The new API |
hash Mode router
The default working mode of the router is hash Pattern , for example http://localhost:8080/#/demo
No extra configuration is required for this routing mode , As follows :
router/index.js
import VueRouter from "vue-router";
import Demo from "@/views/demo";
const routers = [
{
path:"/demo",
component:Demo
}
]
const router = new VueRouter({
routes:[...routers]
})
export default router;
history Mode router
This routing mode is common in projects , for example http://localhost:8080/demo
For this routing mode front-end and nginx All need to be configured
router/index.js
import VueRouter from "vue-router";
import Demo from "@/views/demo";
const routers = [
{
path:"/demo",
component:Demo
}
]
const router = new VueRouter({
mode: 'history', // It is amended as follows history route
// base: '', // Set the base path for the route
routes:[...routers]
})
export default router;
common bug
One : Report errors :Unexpected token ‘<‘ Error resolution
The reason for the error
The error of the introduced resource path
bug solve
Method 1 : Adjust the routing mode ;
Method 2 : Do not change the routing mode , Change profile :
1: Modify the basic when deploying the application package url, Change absolute path to relative path ( Or not set publicPath, The default is relative path )
:2: modify vue.config.js, The specific changes are as follows :
module.exports = {
publicPath: '/'
}
Two : Report errors :We’re sorry but XXX doesn’t work properly without JavaScript enabled
The reason for the error
First check the requested url Whether it is right , Whether there are fewer or more slashes , for example http://localhost:3000/api/add Change into http://localhost:3000/api//add
bug solve
Method 1 : Adjust the routing mode , Change it to hash route ;
Method 2 :publicPath: ‘/’
Method 3 : modify nginx As follows :
location ^~ /api/ {
#api: Back end service proxy path ( According to the actual situation of the project )
proxy_pass http://localhost:3000/; # The real address of the back-end service
}
3、 ... and : When the routing mode is history When routing , Refresh the page 404
The reason for the error
history In the routing mode, requests will be sent to the server , But the server does not have this resource file , So it's going back to 404
bug solve
modify nginx To configure
Scheme 1 is as follows :
location / {
root /usr/share/nginx/html/dist; # Deposit dist
index index.html index.htm;
try_files $uri $uri/ /index.html; # solve history Route page refresh 404 problem
}
Plan 2 is as follows :
location /{
root /usr/share/nginx/html/dist; # Deposit dist
index index.html index.htm;
# solve history Route page refresh 404 problem
if (!-e $request_filename) {
rewrite ^/(.*) /index.html last;
break;
}
}
边栏推荐
- 图书管理系统(山东农业大学课程设计)
- [solution] educational codeforces round 82
- Recommended practice sharing of Zhilian recruitment based on Nebula graph
- Yyds dry goods inventory student attendance system based on QT design
- Boot transaction usage
- 数学分析_笔记_第6章:一元函数的Riemann积分
- Usage of group by
- Compress words (kmp/ string hash, double hash)
- Text intelligent expansion and contraction control of swiftui text component (tutorial includes source code)
- Effectively use keywords to increase Amazon sales
猜你喜欢
电脑管理员权限在哪里可以打开
IDEA中设置背景图片(超详细)
Classifier visual interpretation stylex: Google, MIT, etc. have found the key attributes that affect image classification
头条 | 亚控科技产品入选中纺联《纺织服装行业数字化转型解决方案重点推广名录》
The light of ideal never dies
Understand the key technology of AGV -- the difference between laser slam and visual slam
数据安全产业系列沙龙(三)| 数据安全产业标准体系建设主题沙龙
Mobile web development learning notes - Layout
JS learning notes - operators
mysql min() 求某条件下最小的值出现多个结果
随机推荐
Practice of traffic recording and playback in vivo
[Xiaobai chat cloud] suggestions on container transformation of small and medium-sized enterprises
SSM integration exception handler and project exception handling scheme
PCL 点云镜像变换
数仓中的维度表与事实表
Text intelligent expansion and contraction control of swiftui text component (tutorial includes source code)
Summary of monthly report | list of major events of moonbeam in June
PyC file decompile
Register as a harmonios developer and install deveco studio 3.0 beta2 for harmonios
做机器视觉哪个软件好?
通过两级网关设计来路由服务网格流量
Vscade set multi line display of tab
Yyds dry goods inventory student attendance system based on QT design
Leetcode -- number of palindromes
Various entanglements between qvariant and Jason -- QT
Idea jar package conflict troubleshooting
day4
Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!
仙人掌之歌——投石问路(2)
华为云服务器安装mysqlb for mysqld.service failed because the control process exited with error code.See “sys