简体   繁体   English

错误:在此服务器上找不到请求的URL(在刷新页面时,页面在构建后未加载)

[英]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.

相关问题 当我们重定向页面时,它显示 404 Page Not Found The page is not found in codeigniter using ngnix - when we redirecting the page it showing 404 Page Not Found The page you requested was not found in codeigniter using ngnix 显示未找到服务器错误页面时,如何刷新页面/框架? - How can I refresh a page/frame when a server not found error page is displayed? 当将codeigniter与php和mysql一起使用时,在此服务器上找不到请求的URL错误 - The requested URL was not found on this server error when using codeigniter with php and mysql htaccess规则,如果找不到请求的URL,则在页面上进行重定向 - htaccess rule for redirect on a page if requested url not found xampp上的Codeigniter错误:在此服务器上找不到请求的URL - Codeigniter on xampp error: The requested URL was not found on this server .htaccess 错误:在此服务器上找不到请求的 URL - .htaccess error : The requested URL was not found on this server 登录codeigniter后将url重定向到请求的页面 - redirect url to requested page after login codeigniter CodeIgniter“找不到您请求的页面。”错误? - CodeIgniter “The page you requested was not found.” error? 在此服务器上未找到请求的 URL - Requested URL was not found on this server 在此服务器上找不到请求的网址 - The requested url was not found on this server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM