Recently, I have started playing with angular 2. It\'s awesome so far. So, i have started a demo personal project for the sake of learning using angular-cli
. <
You need to add RouterModule
to imports
of every @NgModule()
where components use any component or directive from (in this case routerLink
and <router-outlet>
.
declarations: []
is to make components, directives, pipes, known inside the current module.
exports: []
is to make components, directives, pipes, available to importing modules. What is added to declarations
only is private to the module. exports
makes them public.
See also https://angular.io/api/router/RouterModule#usage-notes
When nothing else works when it should work, restart ng serve. It's sad to find this kind of bugs.
I'll add another case where I was getting the same error but just being a dummy. I had added [routerLinkActiveOptions]="{exact: true}"
without yet adding routerLinkActive="active"
.
My incorrect code was
<a class="nav-link active" routerLink="/dashboard" [routerLinkActiveOptions]="{exact: true}">
Home
</a>
when it should have been
<a class="nav-link active" routerLink="/dashboard" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">
Home
</a>
Without having routerLinkActive
, you can't have routerLinkActiveOptions
.
I am running tests for my Angular app and encountered error Can't bind to 'routerLink' since it isn't a known property of 'a'
as well.
I thought it might be useful to show my Angular dependencies:
"@angular/animations": "^8.2.14",
"@angular/common": "^8.2.14",
"@angular/compiler": "^8.2.14",
"@angular/core": "^8.2.14",
"@angular/forms": "^8.2.14",
"@angular/router": "^8.2.14",
The issue was in my spec
file. I compared to another similar component spec
file and found that I was missing RouterTestingModule
in imports
, e.g.
TestBed.configureTestingModule({
declarations: [
...
],
imports: [ReactiveFormsModule, HttpClientTestingModule, RouterTestingModule],
providers: [...]
});
});
You are missing either the inclusion of the route package, or including the router module in your main app module.
Make sure your package.json has this:
"@angular/router": "^3.3.1"
Then in your app.module import the router and configure the routes:
import { RouterModule } from '@angular/router';
imports: [
RouterModule.forRoot([
{path: '', component: DashboardComponent},
{path: 'dashboard', component: DashboardComponent}
])
],
Update:
Move the AppRoutingModule to be first in the imports:
imports: [
AppRoutingModule.
BrowserModule,
FormsModule,
HttpModule,
AlertModule.forRoot(), // What is this?
LayoutModule,
UsersModule
],
In my case I just need to import my newly created component to RouterModule
{path: 'newPath', component: newComponent}
Then in your app.module
import the router and configure the routes:
import { RouterModule } from '@angular/router';
imports: [
RouterModule.forRoot([
{path: '', component: DashboardComponent},
{path: 'dashboard', component: DashboardComponent},
{path: 'newPath', component: newComponent}
])
],
Hope this helps to some one !!!