首页 > 系统相关 >vue-router之hash与history,以及nginx配置

vue-router之hash与history,以及nginx配置

时间:2023-06-26 12:22:18浏览次数:32  
标签:index vue hash nginx html root history

本篇讲解前端项目的路由模式(以vue-router为例),以及history模式下的项目部署问题。

vue-router的路由模式可以通过指定mode属性值控制,可选值:"hash" 、"history"、 "abstract" , 默认:"hash" (浏览器环境) , "abstract" (Node.js 环境)

const router = new VueRouter({
  mode: 'history',
  routes: [...]
})

路由表里的兜底配置

image.png

hash与history

Hash模式

通过 onhashchange 方法监听hash的改变来实现

  • Hash模式是基于锚点,以及onhashchange事件
  • URL中#后面的内容作为路径地址
  • 监听hashchange事件
  • 根据当前路由地址找到对应组件重新渲染

History模式

通过 onpopstate 方法监听history的改变来实现

  • History模式是基于HTML5中的History API
  • 通过history.pushState()方法改变地址栏 IE 10 以后才支持
  • 监听popstate事件
  • history.replaceState()
  • 根据当前路由地址找到对应组件重新渲染

image.png

History模式的使用,以及nginx配置

  • History 需要服务器的支持
  • 单页应用中,服务端不存在http://www.testurl.com/login这样的地址,会返回找不到该页面
  • 在服务端应该除了静态资源外都返回单页应用的index.html,比如:http://www.testurl.com/login.html

history需要服务器支持,我们使用node或nginx
http://localhost:8080/main/home

nginx处理方式

在nginx的html根目录部署一个项目,然后新开一个文件夹,部署另一个项目,nginx.conf

location / {
  root    html;
  index   index.html index.htm;
  try_files $uri $uri/ /index.html
}

try_files:

  • $uri: 当前请求路由
    这句话意思是尝试请求当前路由,如果请求不到,就返回当前目录下的index.html

nginx root 和 alias 的区别

location  /i/ {
 alias   /spool/w3/images/;
}
# 

标签:index,vue,hash,nginx,html,root,history
From: https://www.cnblogs.com/all-smile/p/17505327.html

相关文章

  • vue的路由(更新中)
    路由含义及原理路由(router)就是对应关系。地址与页面上组件的对应关系。Hash地址与组件之间的对应关系!锚链接锚链接不会使页面刷新,但有浏览历史。(AnchorLink)是指在网页中通过链接跳转到同一页面的不同位置的技术。它可以让用户通过点击链接或者页面内部的锚点,直接跳转到页面......
  • Nginx配置max_fails fail_timeout 不起作用 - stub_status - 调试 nginx --with-deb
    0.stub_statusconfigurearguments:--prefix=/usr/local/tengine--with-http_realip_module--with-http_gzip_static_module--with-pcre--with-http_stub_status_module--with-http_ssl_module--add-module=/opt/nginx-goodies-nginx-sticky-module-ng[root@slave1con......
  • Vue项目难点解析---分页器静态组件【原理】
    分页器:为什么使用分页器?答案:按需加载为啥不直接使用ElementUI啥的第三方组件呢?答案:掌握自定义分页功能,更好的自定义分页规则呢,实现功能。实现原理:分页器实现条件?知道当前第几页:pageNo知道分页器一共需要展示多少条数据:total知道每一页需要展示数据个数:pageSize知道连续的页码数......
  • Nginx-PHP优化设置 + lnmp调优的关键影响因素 + php-fpm + nginx返回码 + tcp调优 +
    最大文件描述符Linux内核本身有文件描述符最大值的**,你可以根据需要更改:系统最大打开文件描述符数:/proc/sys/fs/file-max临时性设置:echo1000000>/proc/sys/fs/file-max永久设置:修改/etc/sysctl.conf文件,增加fs.file-max=10000002、用户级设置vi/etc/security/limits.confhttp......
  • elk 入门 - 分析nginx日志 + json格式 + 有调试的意识 + elk7.2.0
    1.本次采用的一台主机,将所有的软件安装一台上进行测试工作。2.安装部署:https://blog.51cto.com/hwg1227/22999953.简单调试输出rubydebuginput{file{path=>"/usr/local/log_test/*/*/*.log"start_position=>"beginning"}}output{e......
  • 配置文件 proxy_set_header -发往后端服务器的请求头---- nginx日志设置级别调试技巧
    http{includemime.types;default_typeapplication/octet-stream;sendfileon;#tcp_nopushon;#keepalive_timeout0;keepalive_timeout65;server_tokensoff;log_formatmain'$remote_addr-$remote_user[$time_local]......
  • Nginx 根据请求参数代理到指定网址
    1、参考nginx中将某一个请求的路径重定向到其它网址nginx:[emerg]unknowndirective"if($request_uri"in2、小坑if与($间需要有空格。判断请求路径中,是否包含/adminif($request_uri~'/admin'){return404;}3、配置location/xxxxx/client/register{......
  • VUE3中实现“收起”“展开”功能
    《好记性不如烂笔头系列》<template><van-row><divclass="roadshowDescValueDiv"><divref="desContent"class="desContent":class="{'show-all':desShowAll}">......
  • vue中的 ref 和 $refs
    在Vue中,ref是一个用于给元素或组件添加引用的特殊属性。通过在元素上添加ref属性,可以在Vue组件实例或父组件中通过引用访问该元素或组件的实例。具体来说,ref属性有两种用法:1.给普通元素添加引用:<template><div><inputref="myInput"type="text"><button@cl......
  • jeecg2-VUE-全局替换字体(鸿蒙、、、)
    html,body{font-family:-apple-system,BlinkMacSystemFont,'SegoeUI','PingFangSC',"HarmonyOSSansSC",'HiraginoSansGB','MicrosoftYaHei','HelveticaNeue',Helvetica,Arial,sans-serif......