Hi I am trying to load routes dynamically returned by API server. I am calling a http service in app.component to get routing data. But I get error "No Component factory found for component".I have my components added in entryComponent here is my code.
error:
Uncaught (in promise): Error: No component factory found for ParentComponent. Did you add it to @NgModule.entryComponents? Error: No component factory found for ParentComponent. Did you add it to @NgModule.entryComponents? at noComponentFactoryError (core.es5.js:3202) at CodegenComponentFactoryResolver.webpackJsonp.../../../core/@angular/core.es5.js.CodegenComponentFactoryResolver.resolveComponentFactory (core.es5.js:3267) at RouterOutlet.webpackJsonp.../../../router/@angular/router.es5.js.RouterOutlet.activateWith (router.es5.js:5614) at ActivateRoutes.webpackJsonp.../../../router/@angular/router.es5.js.ActivateRoutes.activateRoutes (router.es5.js:4749) at router.es5.js:4700
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import { routingComponents,AppRoutingModule } from './app.routing.module';
import { ParentComponent } from './parent.component';
import { RouterService } from './router.service';
@NgModule({
declarations: [
AppComponent,
routingComponents
],
imports: [
BrowserModule,
AppRoutingModule,HttpModule
],
providers: [RouterService],
bootstrap: [AppComponent],
entryComponents:[ParentComponent]
})
export class AppModule { }
app.routing.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { ParentComponent } from './parent.component';
const routes: Routes = []
@NgModule({
imports: [RouterModule.forRoot(routes, { useHash: true })],
exports: [RouterModule]
})
export class AppRoutingModule {
}
export const routingComponents =
[ParentComponent]
router.service.ts
import { Injectable } from '@angular/core';
import { Http, Response, RequestOptions, URLSearchParams } from
'@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class RouterService {
constructor(private http: Http) {}
getRoutes(){
return this.http.get("assets/json/router.json")
.map((response:Response)=>{
const extracted = response.json();
console.log("router service",extracted);
return extracted;
})
}
}
app.component.ts
import { Component, OnInit } from '@angular/core';
import { RouterService } from './router.service';
import { Router } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent implements OnInit {
constructor(private router: Router, private routerService: RouterService) {
}
ngOnInit() {
this.routerService.getRoutes().subscribe(response => {
this.router.resetConfig(response);
})
}
}
app.component.html
<router-outlet></router-outlet>