当前位置:网站首页>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;
}
}
边栏推荐
- AWS virtual machine expansion
- 一文读懂AGV的关键技术——激光SLAM与视觉SLAM的区别
- sql解决连续登录问题变形-节假日过滤
- Data Lake (11): Iceberg table data organization and query
- Add user-defined formula (time sharing t+0) to mobile app access as an example
- idea jar包冲突排查
- Text intelligent expansion and contraction control of swiftui text component (tutorial includes source code)
- OSPF - detailed explanation of NSSA area and full NSSA area (including configuration command), LSA type 7 lsa-7
- 电脑管理员权限在哪里可以打开
- Compress words (kmp/ string hash, double hash)
猜你喜欢
SSM整合-异常处理器及项目异常处理方案
手机app通达信添加自定义公式(分时T+0)为例子讲解
分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation
Add user-defined formula (time sharing t+0) to mobile app access as an example
PCL 点云镜像变换
Yyds dry inventory method of deleting expired documents in batch
触发器:Mysql实现一张表添加或删除一条数据,另一张表同时添加
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
华为云服务器安装mysqlb for mysqld.service failed because the control process exited with error code.See “sys
随机推荐
Win11应用商店无法加载页面怎么办?Win11商店无法加载页面
Mysql database mysqldump why there is no statement to create a database
Bean configuration override in boot
Introduction to database system Chapter 1 short answer questions - how was the final exam?
TypeScript数组乱序输出
Seal Library - installation and introduction
win10系统升级一段时间后,内存占用过高
去除router-link中的下划线
潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)
MySQL min() finds the minimum value under certain conditions, and there are multiple results
[Xiaobai chat cloud] suggestions on container transformation of small and medium-sized enterprises
Mobile web development learning notes - Layout
JS learning notes - data types
Source code look me
Set the background picture in the idea (ultra detailed)
OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation
仙人掌之歌——投石问路(3)
sim2real环境配置教程
What are the necessary functions of short video app development?
Aike AI frontier promotion (2.15)