RouterModule:路由器实例

RouterModule: Router Instance

我希望在我的子模块中访问 RouterModule 指令 (routerLink),但我没有任何路由可以添加到 forChild 方法。 我应该以哪种方式在我的@NgModule class 中导入 RouterModule 而无需获取 Router 的另一个实例(已经通过 RouterModule.forRoot(ROUTES) 在根应用程序模块中实例化):

@NgModule({
   imports: [RouterModule.forChild([])]
})

@NgModule({
  imports: [RouterModule]
})

两者似乎都适用于我的应用程序。我只是不确定服务实例是否发生了两次。

我已经阅读了文档的这两个条目:

The result is that the root application module imports RouterModule.forRoot(...) and gets a Router, whereas all route components import RouterModule which does not include the Router.[1]

并且:

For submodules and lazy loaded submodules the module should be used as follows[2]:

  @NgModule({
    imports: [RouterModule.forChild(ROUTES)]
  })
  class MyNgModule {}

如果不导入 RouterModule,就不能有 routerLink 指令。你必须简单地将它导入你的子模块而不调用 RouterModule.forChild() 方法。

为了更好地理解forRoot()forChild(),请参考以下链接:

Avoiding common confusions with modules in Angular