0

I am developing a Web application using Angular 5. I am new to Angular with Typescript. Now I am configuring the route for my application modules. But it is not working and throwing an error.

This is my index.html file

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Web</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
  <router-outlet></router-outlet>
</body>
</html>

I created an app-routing.module.ts file under the app folder.

    import { NgModule } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';
    import { AppComponent } from './app.component';


    const routes: Routes = [ 
        { path: '', loadChildren: './web/web.module#WebModule' },
    ];

    @NgModule({
        imports: [RouterModule.forRoot(routes)],
        exports: [RouterModule]
    })
    export class AppRoutingModule {}


Import that routing class in the app.module.ts file

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { RouterModule, Route } from '@angular/router';
import { AppRoutingModule } from './app-routing.module'

import { AppComponent } from './app.component';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    AppRoutingModule,
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Then created another module, web/web.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HomeComponent } from './home/home.component';
import { WebRoutingModule  } from './web-routing.module';

@NgModule({
  imports: [
    CommonModule,
    WebRoutingModule
  ],
  declarations: [HomeComponent]
})
export class WebModule { }

This is my web-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';

const routes: Routes = [
    { path: '', component: HomeComponent }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})
export class WebRoutingModule { }

As you can see I am rendering HomeComponent. I created home component user web/home/ folder.

According to my code, if I access my website just like this, localhost:4200, it should render the home component HTML in the router-out in the index.html file. Now it is not rendering the home component HTML. It is rendering the index.html but without home, component replaced in the router-outlet. So, what is wrong with my code and how can I fix it?

2 Answers 2

8

<router-outlet></router-outlet> has to be in the markup of app-home not in the index.html file for it to work

I have just set up routing in one of my projects.

My app.component.ts looks like this

import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent {
}

My app.component.html looks like this

<router-outlet></router-outlet>

My app-routing.module.ts look like this

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

// Layouts
import { MasterComponent } from './components/layouts/master/master.component';
// Main Pages
import { HomeComponent } from './components/pages/home/home.component';
import { PageNotFoundComponent } from './components/pages/page-not-found/page-not-found.component';

const routes: Routes = [
  { path: '', component: MasterComponent, children: [
    { path: '', component: HomeComponent },
  ] },
  { path: '**', component: PageNotFoundComponent },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

Also my index.html file looks like this

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Office</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Your welcomes, I've set up Routes within angular on loads of different projects now. Glad i could help
0

Try providing your web.module in app.module's imports array

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.