Ref1: http://angularjs.blogspot.com/2016/08/angular-2-rc5-ngmodules-lazy-loading.html Ref2: https://angular.io/docs/ts/latest/guide/ngmodule.html #! # imports
Problem 1: Import / Export NgModel
import { HomeComponent } from './Home/home.module.ts' // HomeComponent not found
home.module.ts exports a HomeModule , not a HomeComponent .
Exporting @NgModule creates the Angular2 functions of these export components available for the import module. functions can be template directives, selector, injection service, etc.
The function of the home component is its selector. Therefore, importing HomeModule into app.module will make the HomeComponent home selector available to any app.module component.
In order for HomeModule to export HomeComponent explicitly, index.js and index.d.ts are needed. (Inspired by Fabio Antune's answer)
Use home selector
To use this, exports: [ HomeComponent ] is required in home.module.ts.
app.module.ts
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { HomeModule } from './Home/home.module'; import { routing } from './app.routing'; @NgModule({ imports: [ BrowserModule, HomeModule, routing], declarations: [ AppComponent], bootstrap: [AppComponent] }) export class AppModule { }
app.component.ts using the home selector
import {Component} from '@angular/core'; @Component({ selector: 'app-component', template: ` <h1>{{title}}</h1> <home></home>` }) export class AppComponent { title = 'APP'; }
Use HomeComponent
To use HomeComponent directly, we need to add index.js and index.d.ts to. / Home
./Home/index.js
exports.HomeModule = require('./home.module').HomeModule; exports.HomeComponent = require('./home.component').HomeComponent;
./Home/index.d.ts
exports * './home.module'; exports * from './home.component';
Then import the Home Module, e.g. npm package
app.routing.ts
// We are importing the module directory, not ./Home/home.module.ts import { HomeComponent } from './Home'; import { Routes, RouterModule } from '@angular/router' const appRoutes: Routes = [ { path: '', component: HomeComponent } ]; export const appRoutingProviders: any[] = [ ]; export const routing = RouterModule.forRoot(appRoutes);
Problem 2: Routing (for the child module)
To go to the module, the child module must have its own router settings.
Use loadChildren in parent routing (app.routing.ts).
Use RouterModule.forChild in child routing (home.routing.ts).
app.module.ts
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { HomeModule } from './Home/home.module'; import { routing } from './app.routing'; @NgModule({ imports: [ BrowserModule, HomeModule, routing], declarations: [ AppComponent], bootstrap: [AppComponent] }) export class AppModule { }
app.component.ts
import {Component} from '@angular/core'; @Component({ selector: 'app-component', template: ` <h1>{{title}}</h1> <nav> <a routerLink="/home" routerLinkActive="active">Home</a> </nav> <router-outlet></router-outlet>` }) export class AppComponent { title = 'APP'; }
app.routing.ts
import { Routes, RouterModule } from '@angular/router'; const appRoutes: Routes = [ { path: '', redirectTo: '/home', pathMatch: 'full' }, { path: 'home', loadChildren: './Home/home.module' } ]; export const appRoutingProviders: any[] = [ ]; export const routing = RouterModule.forRoot(appRoutes);
Home /home.module.ts
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { HomeComponent } from './home.component'; import { routing } from './home.routing'; @NgModule({ imports: [ BrowserModule, routing], declarations: [ HomeComponent] }) export class HomeModule { }
Home /home.routing.ts
import { Routes, RouterModule } from '@angular/router'; import { HomeComponent } from './home.component'; const homeRoutes: Routes = [ { path: '', redirectTo: '/home', pathMatch: 'full' }, { path: 'home', component: HomeComponent } ]; export const appRoutingProviders: any[] = [ ]; export const routing = RouterModule.forChild(homeRoutes);