Angular 8 - URL loads with routerLink but doesn't show when directly access in the browser in LOCALHOST
Asked Answered
S

3

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
Somite answered 15/8, 2019 at 14:27 Comment(13)
Could you please share the template of your AppComponent?Hillard
Hello, @WillAlexander I have updated the question with details as it was a bit explanatory, thanks !!Somite
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 URLSomite
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?Hillard
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 issueSomite
How do you start your server?Sialkot
I am using ng serve to start in the local system, It was well working before initially and not sure what changed suddenlySomite
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 aswellSomite
@JBNizet , Did you find anything suspicious?Somite
I was wondering if you were actually using the CLI or not. Are you sure you don't have any compilation issue?Sialkot
No surpsrisely I don't see any compilation issueSomite
I supouse you import the AppRoutingModule in your app.moduleColored
Did you find a solution, without using the hash?Calculus
B
8

On server we can use hashStrategy for routing.

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

Burlington answered 15/12, 2019 at 17:53 Comment(2)
Simple, effective solutionDrudgery
Thanks Guy, It solved my problem after much searching on google.Baruch
D
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

Dru answered 17/7, 2020 at 16:27 Comment(0)
V
0

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

Veinule answered 17/9, 2020 at 2:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.