history路由会请求服务器,因此需要服务器配合返回一个固定的index.html页面
以nginx配置为例:
location / {
try_files $uri $uri/ /index.html;
}
以上面的 http://www.example.com/post 为例,$uri 会匹配到 post,nginx 发现 dist 目录下下面没有 post 这个文件,也没有 post 这个文件夹,所以最后会返回 dist 目录下的 index.html。这样,index.html 被浏览器加载之后,前端路由就会工作,将用户需要的资源加载出来。而我们 build 出来的 css,js 文件,由于可以被 nginx 正确找到,则不会受到影响。



