7

I have developed the Angular 8 application and I am using the routerLink to navigate the components which work fine without any issue but when I enter the same URL directly in the browser it doesn't show anything and in the console, I am seeing the errors as below

enter image description here

For Example, I open the homepage http://localhost:4200/home and I have added the routerLink here to go to http://localhost:4200/about and I will be able to view successfully but if I enter the URL http://localhost:4200/about directly in the ULR nothing shows

I have taken care of the app-routing.model.ts

const routes: Routes =
    [
        { path: 'home', component: HomeComponent },
        { path: 'about', component: AboutComponent },
        { path: '**', redirectTo: '/home', pathMatch: 'full' }
    ];
    @NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})

And I have header.component.ts to handle the navigation bar so I have added the <router-outlet></router-outlet> in the header.component.html and then I have included the selector of header.component.ts in app.component.ts <app-header></app-header> and then in finally index.html I have included the selector of app.component.ts i.e <app-root></app-root>

Can you please tell me if there is something wrong.

Before asking this question I have gone through below and nothing helps

  1. RouterLink does not work
  2. Angular 2 Routing Does Not Work When Deployed to Http Server
  3. Angular2 routing - url doesn't change and page doesn't load
Mahesh G
  • 1,226
  • 4
  • 30
  • 57
  • 1
    Could you please share the template of your AppComponent? – Will Alexander Aug 15 '19 at 14:32
  • Hello, @WillAlexander I have updated the question with details as it was a bit explanatory, thanks !! – Mahesh G Aug 15 '19 at 14:37
  • And I am seeing the errors as `Failed to load resource: the server responded with a status of 404 (Not Found)` in the console when I directly access the URL – Mahesh G Aug 15 '19 at 14:42
  • Have you tried restarting the development server? Have you modified the `base href` at all? Have you tried putting your `router-outlet` in the AppComponent template, as that would make more sense? – Will Alexander Aug 15 '19 at 14:46
  • 1
    Yeah I have tried putting the router-outlet to app.component and also I have ` ` in the index.html file and still the same issue – Mahesh G Aug 15 '19 at 14:50
  • How do you start your server? – JB Nizet Aug 15 '19 at 14:56
  • I am using `ng serve` to start in the local system, It was well working before initially and not sure what changed suddenly – Mahesh G Aug 15 '19 at 14:57
  • And I know many people suggested to use hash in the router module like `imports: [RouterModule.forRoot(routes, { useHash: true })],`, this will work but I don't want to keep # in the URL and my app was working previously without hash aswell – Mahesh G Aug 15 '19 at 15:02
  • @JBNizet , Did you find anything suspicious? – Mahesh G Aug 15 '19 at 15:23
  • I was wondering if you were actually using the CLI or not. Are you sure you don't have any compilation issue? – JB Nizet Aug 15 '19 at 15:32
  • No surpsrisely I don't see any compilation issue – Mahesh G Aug 15 '19 at 15:35
  • I supouse you import the AppRoutingModule in your app.module – Eliseo Aug 15 '19 at 20:30
  • Did you find a solution, without using the hash? – anexo Jun 05 '20 at 14:06

3 Answers3

8

On server we can use hashStrategy for routing.

In routing file just replace RouterModule.forRoot(routes) to RouterModule.forRoot(routes,{useHash : true}).

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
aakash varshney
  • 148
  • 1
  • 5
0

for apache server you can add .htaccess file

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

We have to make sure that inside index.html base href is

Mahen
  • 760
  • 9
  • 12
0

This may be a little odd, but I had a trailing space in a cssclass="class class2 "

Dave
  • 1,179
  • 12
  • 18