繁体   English   中英

具有动态路由的Angular 4动态组件会引发错误

[英]Angular 4 dynamic components with dynamic routing throwing an error

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

config.json

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

```

其中,在调用router.resetConfig(loadedRoutes);之前,将"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

它在@NgModule声明为组件?

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

我发现在解析子级路由的component属性时设置不正确,解决了这个问题就解决了问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM