前言
前面的项目都是使用hash模式,即带#来访问的。
不同模式(h5 history 和 hash #)两种模式之间的切换,在angular中做如下配置即可:
index.html
<base href="/">
‘/’ 后面什么都不填的话 那就是你Nginx的目录名
app.module.ts
import { LocationStrategy, HashLocationStrategy, PathLocationStrategy } from '@angular/common';
providers: [
// { provide: LocationStrategy, useClass: HashLocationStrategy } // hash模式
{provide: LocationStrategy, useClass: PathLocationStrategy} // 无#及h5 history模式
],
hash模式使用
在默认通过使用hash模式发布网站之后,效果如下:
这种模式就是不好看,并且seo方面也有问题。其他没什么大的问题,部署之后刷新也没问题。
H5 history模式
在使用无哈希,即使用history模式的时候,只需要更改app.module.ts中的配置即可。
但是发布之后会存在刷新404问题。所以就需要使用nginx来解决。
Nginx 要配置一下
try_files $uri $uri/ /index.html; # #try_files先寻找名为 $uri 文件,没有则寻找 $uri/ 文件,再没有就寻找/index.html