angular4.0项目build发布后,刷新页面报错404

时间:2023-03-09 03:16:28
angular4.0项目build发布后,刷新页面报错404

angular4.0项目执行npm run build后,进入页面正常显示。

但是当刷新页面时,报错404,页面未找到。

出现这个问题的原因,应该是找不到路由地址导致的,然后找到了下面的解决方案。

找到app.module.ts文件,这个是根模块。在模块中加入HashLocationStrategy和LocationStrategy服务。

// 1、引入HashLocationStrategy、LocationStrategy服务
import {HashLocationStrategy, LocationStrategy} from '@angular/common'; // 2、注入服务
@NgModule({
declarations: [ ],
imports: [ ],
providers: [
{provide: LocationStrategy, useClass: HashLocationStrategy}
],
bootstrap: [AppComponent]
})

ok,就是酱紫,完美解决。