Can't bind to 'routerLink' since it isn't a known property

后端 未结 9 849
别那么骄傲
别那么骄傲 2020-12-12 14:16

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. <

相关标签:
9条回答
  • 2020-12-12 14:37

    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

    0 讨论(0)
  • 2020-12-12 14:40

    When nothing else works when it should work, restart ng serve. It's sad to find this kind of bugs.

    0 讨论(0)
  • 2020-12-12 14:46

    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.

    0 讨论(0)
  • 2020-12-12 14:46

    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: [...]
        });
      });
    
    0 讨论(0)
  • 2020-12-12 14:47

    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
      ],
    
    0 讨论(0)
  • 2020-12-12 14:50

    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 !!!

    0 讨论(0)
提交回复
热议问题