How do you import the routerLink directive exclusively in Angular 2
Problem
I'm making a mon module for my app and I use routerLink everywhere but nothing else in the RouterModule so I'm wanting a way to get access to the directive without importing everything.
What I Know
I know to get the routerLink directive you need to import the RouterModule, like so.
import { RouterModule } from '@angular/router';
I also know that all modules just import, declare, and export ponents, service and so on. So in theory the routerLink should be able to be imported directly but I can't find out how.
How do you import the routerLink directive exclusively in Angular 2
Problem
I'm making a mon module for my app and I use routerLink everywhere but nothing else in the RouterModule so I'm wanting a way to get access to the directive without importing everything.
What I Know
I know to get the routerLink directive you need to import the RouterModule, like so.
import { RouterModule } from '@angular/router';
I also know that all modules just import, declare, and export ponents, service and so on. So in theory the routerLink should be able to be imported directly but I can't find out how.
Share Improve this question asked Feb 10, 2017 at 14:45 Michael WarnerMichael Warner 4,2573 gold badges24 silver badges48 bronze badges 3-
2
In your app.ponent I guess you have declared some routes using something like
RouterModule.forRoot(ROUTES, {useHash: false}),
. That basically provides all the needed router directives for your app. Just addRouterModule
to your mon modules as I would guess routing is going to be a mon thing in your app. You will always need theRouterModule
to be present to use its directives, no point in trying to just import the routerLink directive with no RouterModule? – rtn Commented Feb 10, 2017 at 14:58 - you've got a very good point – Michael Warner Commented Feb 10, 2017 at 15:15
- I don't think you can use routerLink without importing the whole RouterModule. – Ben Richards Commented Feb 10, 2017 at 15:55
1 Answer
Reset to default 5Now in 2023 you can import RouterLink):
import { RouterLink } from '@angular/router';