Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 routes not working after building the project

These are my routes:

import { Routes } from '@angular/router';
import {WelcomeComponent} from "./welcome/welcome.component";
import { LoginComponent } from './login/login.component';

export const routes: Routes = [
  {path: '', component: WelcomeComponent},
  {path: 'login', component: LoginComponent},
  {path: '**', component: WelcomeComponent}
];

I buid my project using ng buid.

When I enter a not defined path, I expect the application to redirect to '/' path as it happens during development, but I get an 404 Error.

I get the same error even when I manually enter /login URL.

What am I missing?

like image 466
Jona Avatar asked May 19 '17 08:05

Jona


People also ask

How do I enable Angular routing?

Add a default routelink To make the application navigate to the dashboard automatically, add the following route to the routes array. content_copy { path: '', redirectTo: '/dashboard', pathMatch: 'full' }, This route redirects a URL that fully matches the empty path to the route whose path is '/dashboard' .

Can not match any routes Angular?

This generally occurs when there is a mismatch in the routes specified, or a component which is not present in our routing configuration, or if there is the use of multiple routes in our angular application which is not properly configured.

What does ActivatedRoute do in Angular?

ActivatedRoutelink. Provides access to information about a route associated with a component that is loaded in an outlet. Use to traverse the RouterState tree and extract information from nodes.

What are the Angular 2 route guards?

Angular route guards are interfaces provided by Angular which, when implemented, allow us to control the accessibility of a route based on conditions provided in class implementation of that interface. Here are some types of Angular guards: CanActivate, CanActivateChild, CanLoad, CanDeactivate and Resolve.


1 Answers

As @Milad stated I had to redirect all get request to index.html. Adding .htacces file solved my issue:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]
</IfModule>
like image 148
Jona Avatar answered Oct 20 '22 10:10

Jona