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
asked Aug 15, 2019 at 14:27
13
  • 1
    Could you please share the template of your AppComponent? Commented Aug 15, 2019 at 14:32
  • Hello, @WillAlexander I have updated the question with details as it was a bit explanatory, thanks !! Commented Aug 15, 2019 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 Commented Aug 15, 2019 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? Commented Aug 15, 2019 at 14:46
  • 1
    Yeah I have tried putting the router-outlet to app.component and also I have ` <base href="/">` in the index.html file and still the same issue Commented Aug 15, 2019 at 14:50

3 Answers 3

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
52.1k193 gold badges61 silver badges101 bronze badges
answered Dec 15, 2019 at 17:53
Sign up to request clarification or add additional context in comments.

2 Comments

Simple, effective solution
Thanks Guy, It solved my problem after much searching on google.
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

answered Jul 17, 2020 at 16:27

Comments

0

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

answered Sep 17, 2020 at 2:47

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.