Angular 4动态组件与动态路由抛出错误

时间:2018-02-01 14:15:22

标签: javascript angular typescript

我有这个路由配置来自服务器并在应用程序引导程序之前加载:

config.json

[{
  "path": "dashboard",
  "component": "SingleComponent",
  "data": {...}
},
{
  "path": "payment",
  "roles": ["normal-user"],
  "children": [{
    "path": "new",
    "component": "SingleComponent",
    "data": {...}
    }
  }]
}

```

其中"SingleComponent"在调用router.resetConfig(loadedRoutes);之前被类的引用替换 直到现在一切正常,如果我加载/dashboard页面正确加载(我需要将组件引用放入@NgModule.entryComponents,因为其他地方没有引用该组件。

如果我导航到/payment/new我会收到此错误

ERROR Error: Uncaught (in promise): Error: No component factory found for SingleComponent. Did you add it to @NgModule.entryComponents?
Error: No component factory found for SingleComponent. Did you add it to @NgModule.entryComponents?
at noComponentFactoryError (core.js:3898)
at CodegenComponentFactoryResolver.resolveComponentFactory (core.js:3962)
at RouterOutlet.activateWith (router.js:6652)
at ActivateRoutes.activateRoutes (router.js:5735)
at eval (router.js:5675)
at Array.forEach (<anonymous>)
at ActivateRoutes.activateChildRoutes (router.js:5674)
at ActivateRoutes.activateRoutes (router.js:5742)
at eval (router.js:5675)
at Array.forEach (<anonymous>)
at noComponentFactoryError (core.js:3898)
at CodegenComponentFactoryResolver.resolveComponentFactory (core.js:3962)
at RouterOutlet.activateWith (router.js:6652)
at ActivateRoutes.activateRoutes (router.js:5735)
at eval (router.js:5675)
at Array.forEach (<anonymous>)
at ActivateRoutes.activateChildRoutes (router.js:5674)
at ActivateRoutes.activateRoutes (router.js:5742)
at eval (router.js:5675)
at Array.forEach (<anonymous>)
at resolvePromise (zone.js:824)
at resolvePromise (zone.js:795)
at eval (zone.js:873)
at ZoneDelegate.invokeTask (zone.js:425)
at Object.onInvokeTask (core.js:4744)
at ZoneDelegate.invokeTask (zone.js:424)
at Zone.runTask (zone.js:192)
at drainMicroTaskQueue (zone.js:602)
at ZoneTask.invokeTask [as invoke] (zone.js:503)
at invokeTask (zone.js:1540)

但实际上那是我的模块:

...

  entryComponents: [ // components used by remote loaded routes
    SingleComponent,
  ],
})

发生了什么事?有任何想法吗? 我找不到任何关于此错误的文档

angular是版本5.0.0

2 个答案:

答案 0 :(得分:0)

它是@NgModule声明的组件吗?

@NgModule({
  declarations: [
    ...
    SingleComponent,
    ...
  ]
});

答案 1 :(得分:0)

我发现在解析过程中,子路径的component属性设置不正确,解决了这个问题解决了问题