最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular 2 : How do you import routerLink exclusively - Stack Overflow

programmeradmin0浏览0评论

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 add RouterModule to your mon modules as I would guess routing is going to be a mon thing in your app. You will always need the RouterModule 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
Add a ment  | 

1 Answer 1

Reset to default 5

Now in 2023 you can import RouterLink):

import { RouterLink } from '@angular/router';
发布评论

评论列表(0)

  1. 暂无评论