原因:NG2+ 会默认不显示URL后面的文件名
解决方案:使用LocationStrategy方式,然后把URL后的# 替换成index.html#
app.module.ts
import {HashLocationStrategy , LocationStrategy} from '@angular/common'; @NgModule({
imports: [ ],
declarations: [
AppComponent,
],
providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.ts(这一步是后端Tomact不设置默认index.html的情况下才需要的)
changeURL() {
var text = window.location.href;
text.toString();
var url = text.replace(/\/#/, "/index.html#");
window.history.pushState({}, "0", url);
}
ngAfterContentChecked(){// 每次做完组件视图和子视图的变更检测之后调用,为了防止循环替换,replace使用全替换模式
this.changeURL();
}
详细内容可浏览下一遍文章《Angular2+URL中的 # 引发的思考》