[英]Error : The requested URL was not found on this server(page not loading after build when we refresh the page)
In developer section (localhost:4200) is working fine. 在开发人员部分(localhost:4200)运行正常。 After completing the build application loading and working fine but url when i type and press enter it is not working.
完成构建应用程序的加载和工作后,但是当我键入并按Enter时,URL不能正常工作。
my router: 我的路由器:
export const appRoutes : Routes = [
{ path: '', redirectTo: 'material', pathMatch: 'full' },
{path:'material',component: MaterialComponent},
];
and my module.ts file 和我的module.ts文件
imports: [
RouterModule.forRoot(appRoutes,{ enableTracing: true }),BrowserModule,FormsModule,HttpModule,MaterialModule,BrowserAnimationsModule,RouterModule
],
Error: 错误:
Why giving custom url is not working after build? 为什么在构建后提供自定义网址不起作用?
The problem is Angular takes care of the url after the dns ( localhost:4200
) part. 问题是Angular在dns(
localhost:4200
)部分之后处理URL。 So when you type localhost:4200/someurl
, The network will query your backend for that full url, and if it doesn't exists you will get a 404. You have 2 options here: 因此,当您键入
localhost:4200/someurl
,网络将在您的后端查询该完整URL,如果不存在该完整URL,您将得到404。这里有2个选项:
1) Use HashLocation Strategy which I don't recommend because it breaks AoT. 1)使用我不建议使用的HashLocation策略,因为它会破坏AoT。
2) Define a redirection function in your back-end code. 2)在您的后端代码中定义重定向功能。 For nodejs we do it like this:
对于nodejs,我们这样做:
app.get('/*', function(req, res, next) {
res.sendFile('index.html', { root: __dirname + '/dist' });
});
Related topic: Angular 2.0 router not working on reloading the browser 相关主题: Angular 2.0路由器无法重新加载浏览器
I have solved using Hash.
我已经解决了使用哈希的问题。 We need to import below modules in module.ts
我们需要在module.ts中导入以下模块
import { HashLocationStrategy, LocationStrategy } from '@angular/common';
and we need to include providers as given below in module.ts file.
并且我们需要在module.ts文件中包含下面提供的提供程序。
providers: [AppCommonService,{provide: LocationStrategy, useClass: HashLocationStrategy}],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.