0
votes

I wish to have access to RouterModule directive(routerLink) in my child Module, tough I don't have any Routes to add to the forChild method. Which way should I import RouterModule in my @NgModule class without getting another instance of Router(That was already instantiated in the root application module through RouterModule.forRoot(ROUTES)):

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

OR

@NgModule({
  imports: [RouterModule]
})

Both seem to work in my app. I'm just not sure if the service instance is happening twice.

I've read these two entries of the documentation:

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]

And:

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

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

1 Answers

1
votes

You cannot have routerLink directive without importing RouterModule. You have to simply import it into your child module without calling RouterModule.forChild() method.

For the better understanding of forRoot() and forChild(), please refer below links:

RouterModule.forRoot(ROUTES) vs RouterModule.forChild(ROUTES)

Avoiding common confusions with modules in Angular