动态路由加载抛出错误“找不到工厂”

时间:2017-09-08 07:55:23

标签: angular angular2-routing angular2-router

您好我正在尝试加载API服务器动态返回的路由。我在app.component中调用http服务来获取路由数据。 但是我收到错误“没有为组件找到组件工厂”。我在entryComponent中添加了我的组件,这里是我的代码。

错误:

未捕获(承诺):错误:未找到ParentComponent的组件工厂。你有没有把它添加到@ NgModule.entryComponents? 错误:未找到ParentComponent的组件工厂。你有没有把它添加到@ NgModule.entryComponents?     at noComponentFactoryError(core.es5.js:3202)     在CodegenComponentFactoryResolver.webpackJsonp ... / .. / .. / core/@angular/core.es5.js.CodegenComponentFactoryResolver.resolveComponentFactory(core.es5.js:3267)     在RouterOutlet.webpackJsonp ... / .. / .. / router / @ angular / boot.es5.js.RouterOutlet.activateWith(router.es5.js:5614)     在ActivateRoutes.webpackJsonp ... / .. / .. / router / @angular / rroutes.es5.js.ActivateRoutes.activateRoutes(router.es5.js:4749)     在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>

0 个答案:

没有答案