解决方案:使用LocationStrategy方式,然后把URL后的# 替换成index.html#
app.module.ts
1 import {HashLocationStrategy,LocationStrategy} from ‘@angular/common‘; 2 3 @NgModule({ 4 imports: [ ], 5 declarations: [ 6 AppComponent, 7 ], 8 providers: [{provide: LocationStrategy,useClass: HashLocationStrategy}], 9 bootstrap: [AppComponent] 10 }) 11 export class AppModule { }
app.component.ts
1 changeURL() { 2 var text = window.location.href; 3 text.toString(); 4 var url = text.replace(/\/#/,"/index.html#"); 5 window.history.pushState({},"0",url); 6 } 7 ngAfterContentChecked(){// 每次做完组件视图和子视图的变更检测之后调用,为了防止循环替换,replace使用全替换模式 8 this.changeURL(); 9 }