允许 Angular 忽略某些 URL

Allow Angular to ignore some URLs

我的 Angular 应用程序在 example.com 上,我的后端在示例上。com/api。我的问题是当我想用 URL https://example.com/api/login/get-cookie 向服务器发送请求时,我收到 HttpErrorResponse 错误。我刚刚在浏览器中输入了相同的 URL,但我将被重定向到主页。所以我可能会收到此错误,因为我的服务器永远无法访问。

如何让 Angular 忽略所有带有 example.com/api/* 的 URL?

我的文件 app-routing.modules.ts 看起来像这样:

const routes: Routes = [
  { path: '', component: HomeComponent, canActivate: [HomeGuard] },
  { path: 'login', component: LoginComponent, canActivate: [LoginGuard] },
  { path: 'register', component: RegisterComponent, canActivate: [LoginGuard] },
  { path: 'search', component: SearchComponent, canActivate: [SearchGuard] },
  { path: 'edit-profile', component: ProfileComponent, canActivate: [SearchGuard] }
];

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

.htaccess:

RewriteEngine On
    # If an existing asset or directory is requested go to it as it is
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
    RewriteRule ^ - [L]
    # If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

添加此重定向会将所有与上述路径不匹配的 url 重定向到主页。或者根据您的喜好,您可以创建一个 404 或错误组件来显示 { path: '**', component: ErrorComponent }

const routes: Routes = [
  { path: '', component: HomeComponent, canActivate: [HomeGuard] },
  { path: 'login', component: LoginComponent, canActivate: [LoginGuard] },
  { path: 'register', component: RegisterComponent, canActivate: [LoginGuard] },
  { path: 'search', component: SearchComponent, canActivate: [SearchGuard] },
  { path: 'edit-profile', component: ProfileComponent, canActivate: [SearchGuard] },
  { path: '**', redirectTo: '/', pathMatch: 'full' }
];