简体   繁体   English

角度路由:使用指定 URL 的直接路径时出错

[英]Angular routing: Error when using direct path of a specified URL

I need some assistance with my routing setup in Angular.我在 Angular 中的路由设置需要一些帮助。 I am running on v12 of Angular.我在 Angular 的 v12 上运行。

I get a 404 Not Found error when trying to hit the direct URL for "register" in the following example URL: somesite.com/ register .尝试在以下示例 URL 中点击“注册”的直接 URL 时出现404 Not Found错误: somesite.com/ register

I'm not sure whether this is a server or Angular issue.我不确定这是服务器问题还是 Angular 问题。 Here is my code for the router module:这是我的路由器模块代码:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { RegistrationPageComponent } from './registration-page/registration-page.component';

const routes: Routes = [
  { path: 'register', component: RegistrationPageComponent },
  // { path: 'register', redirectTo: '/register', pathMatch: 'full' }, // NOTE: I have tried this too
  { path: '', redirectTo: '/register', pathMatch: 'full' },
  { path: '**', redirectTo: '/register', pathMatch: 'full' },
];

@NgModule({
  imports: [RouterModule.forRoot(routes, { onSameUrlNavigation: 'reload' })],
  exports: [RouterModule]
})
export class AppRoutingModule { }

All other aspects of the routing work fine.路由的所有其他方面工作正常。

Thank you in advance!先感谢您!

To me it looks like your nginx server (if you are using one) does not reroute correctly.对我来说,您的 nginx 服务器(如果您使用的是一个)似乎没有正确重新路由。 To handle this you have to change your nginx configuration files, which you can find here:要处理这个问题,您必须更改您的 nginx 配置文件,您可以在此处找到:

Ubuntu 16.04+ / Debian 8+ Ubuntu 16.04+ / Debian 8+

/etc/nginx/conf.d/default.conf

CentOS 7 / Red Hat 7 CentOS 7 / 红帽 7

/etc/nginx/nginx.conf

To rewrite all requests use this method to catch all URIs and forward them to your index.html:要重写所有请求,请使用此方法捕获所有 URI 并将它们转发到您的 index.html:

location / {
     root   /usr/share/nginx/html;
     index  index.html;

     try_files $uri $uri/ /index.html?$args;
}

If you want more complex rerouting behaviours or are using some other server configuration then at least you might know now what to look for.如果您想要更复杂的重新路由行为或使用其他一些服务器配置,那么至少您现在可能知道要寻找什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM