栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > Vue.js

vue-router+nginx 非根路径配置方法

Vue.js 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

vue-router+nginx 非根路径配置方法

vue-router 的默认数据hash模式-使用url的hash来模拟一个完整的URL,于是当URL改变时,页面不会重新加载。

一般情况下,我们不喜欢丑丑的hash,类似于index.html#/matchResult,可以使用路由的history模式。history模式是利用history.pushState API来实现页面跳转。

但是有个问题,在使用nginx的时候,我们需要添加一些配置。

直接配置在根路径下

直接配置在根路径下,访问的时候只用输入http://yoursite.com,在nginx的配置如下

location / {
  try_files $uri $uri/ /index.html;
}
非根路径配置

如果一个域名下有多个项目,那么使用根路径配置就不合适了,我们需要在根路径下指定一层路径,比如说

A项目

http://yoursite.com/A

B项目

http://yoursite.com/B

nginx的配置

    location ^~/A {            alias /XX/A;//此处为A的路径
            index index.html;
            try_files $uri $uri/ /A/index.html;
    }
    location ^~/B {            alias /XX/B;//此处为B的路径
            index index.html;
            try_files $uri $uri/ /B/index.html;
    }

router 配置

A项目

routes: [
    {
      
    },
  ],
  mode: 'history',
  base: '/A',

B项目

routes: [
    {
      
    },
  ],
  mode: 'history',
  base: '/B',

tip: 注意要用alias不能用root



作者:衣桉雁
链接:https://www.jianshu.com/p/0ce1657ec442


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/240658.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号