Vue项目如何开启history模式

ppylox
·3/14/2020·1 阅读
AI 摘要模型根据正文自动生成,仅供参考

Vue项目默认使用hash模式,改为history模式需在router.js中添加`mode: 'history'`;新开页签跳转时去掉URL中的#号;将config/index.js中build的assetsPublicPath由'./'改为'/';并配置nginx的`try_files $uri $uri/ /index.html`避免刷新或不存在地址出现404。若nginx与tomcat指向同一目录,需调整原配置,接口转发可另设location proxy_pass到8080端口。

vue项目里面默认开启的hash模式,开启history模式页面的链接看起来会好一些,开启history需要更改路由里面的模式。

\n\n\n

1、在router.js里面 添加一个属性:

\n\n\n

mode: 'history'

\n\n\n

2、页面跳转不需要修改,如果是新开页签的话需要将#号去掉,比如从列表跳转到详情

\n\n\n

Window.open('/detail?id=' + item.id)

\n\n\n

3、config/index.js里面build的assetsPublicPath需要由'./'改成'/';

\n\n\n

4、为了避免刷新或者不存在的地址出现404,vue官网建议修改nginx配置,nginx需要配置成:

\n\n\n
location / {   \n  try_files $uri $uri/ /index.html; \n} 
\n\n\n

由于我的服务的nginx是通过oneinstack安装的,默认将nginx跟tomcat指向同一个目录,所以需要修改一下原来的配置,稍微有一些差异。

\n\n\n

如果是接口需要默认转发到8080端口:

\n\n\n
location /bootService { \n\n proxy_pass http://127.0.0.1:8080\n\n}\n\nlocation / {\n\n  try_files $uri $uri/ /index.html;\n\n}
\n

相关文章