app.routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './public/home/home.component';
const routes: Routes = [
{
path: 'customers',
loadChildren: '../app/customers/customers.module#CustomersModule'
},
{
path: 'admin',
loadChildren: '../app/admin/admin.module#AdminModule'
},
{
path: 'home',
component: HomeComponent
},
{
path: '**',
redirectTo: 'home',
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
These works fine under the Development environment.
If I directly open localhost:4200/home
then it's landing on the expected page.
However, the one built and deployed using with ng build --prod --base-href=/myngapp/
do works only on www.domain.com/myngapp
if I open the www.domain.com/myngapp/home
directly, then it gives 404 not found error.
Generate an application with routing enabledlink The following command uses the Angular CLI to generate a basic Angular application with an application routing module, called AppRoutingModule , which is an NgModule where you can configure your routes.
Show activity on this post. You can check the current route by injecting the Location object into your controller and checking the path() , like so: class MyController { constructor(private location:Location) {} ...
Linking Routes in HTMLTo add links to one of the routes, use the routerLink directive in HTML. This directive accepts an array. The first parameter is the name of the route, and the second parameter is the parameters that you want to pass with the route.
There are many ways by which you can get a current Route or URL in Angular. You can use the router service, location service or window object to get the path. You can also listen to changes to URL using the router event or URL change event of the location service. Inject the Router and use the URL property.
Angular 6 - Routing. Routing basically means navigating between pages. You have seen many sites with links that direct you to a new page. This can be achieved using routing. Here the pages that we are referring to will be in the form of components. We have already seen how to create a component.
you need to setup something to redirect to your index.html page or change the default location strategy to hash. Yes - you do :) Angular CLI does not automatically add routing to the app just yet therefore you would need to modify app.module.ts file as example below:
Import Location Service from the @angular/common. And use the Location.path () to get the current URL. You can also use the window object to get the path. But remember that it works only if the app is using the browser.
You need to setup proper URL rewriting rules. Angular docs explains how to do it for most popular http servers. You can find details here.
https://angular.io/guide/deployment#server-configuration
What it do, is to simply speaking, tell server to always serve index.html no matter what is the path of the request. eg.
www.domain.com/myngapp/home
-> serve index.html, not home.html or something similar
www.domain.com/myngapp/some/child/routing
->serve index.html and dont try to find some/child/routing.html or php and so on.
You must configure your HTTP server(apache, Nginx or others) to redirect all URI which began with www.domain.com/myngapp/** to the index.html file.
Notice the routes of your application (https://angular6demoamit.000webhostapp.com/) works fine when they are accessed "inside Angular" by interaction with buttons and menus. However, when you try to access the route typing it directlly on the browser, the server doesn't know the URI https://angular6demoamit.000webhostapp.com/home is a route of an Angular application. It'll probabilly try to load the index page of an inexistent home directory.
There isn't a single configuration to solve your problem. It'll depend on your HTTP server and infraestructure. Read the guide of angular which describes some configurations examples to some kinds of HTTP servers: https://angular.io/guide/deployment#routed-apps-must-fallback-to-indexhtml.
I had same issue. if it's working fine in localhost, then you should try this solution. Maybe adding URL rewriting rules in your .htaccess file solve your problem. I found it on http://joeljoseph.net/angular-6-deploy-on-apache-server-by-solving-404-not-found-error-on-page-refresh/
Add this into your .htaccess file.
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
for more detail go to http://joeljoseph.net/angular-6-deploy-on-apache-server-by-solving-404-not-found-error-on-page-refresh/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With