Разрешить Angular игнорировать некоторые URL-адреса

Мое приложение с Angular находится на example.com, а мой сервер — на example.com/api. Моя проблема заключается в том, что я хочу отправить запрос на сервер с URL-адресом https://example.com/api/login/get-cookie, я получаю сообщение об ошибке HttpErrorResponse. Я только что набрал тот же URL-адрес в своем браузере, но меня перенаправит на домашнюю страницу. Так что я, вероятно, получаю эту ошибку, потому что мой сервер никогда не может быть достигнут.

Как я могу разрешить Angular игнорировать все URL-адреса с example.com/api/*?

Мой файл 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

person Reza    schedule 04.02.2020    source источник


Ответы (1)


При добавлении этого перенаправления любые URL-адреса, не соответствующие указанным выше путям, перенаправляются на домашнюю страницу. Или, в зависимости от ваших предпочтений, вы можете создать компонент 404 или Error для отображения вместо { 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' }
];

person Troy Bailey    schedule 04.02.2020