无法绑定到'routerLink',因为它不是已知属性

Uma*_*raz 84 typescript angular2-routing angular

最近,我开始玩角度2.到目前为止它真棒.所以,为了学习使用,我已经开始了一个演示个人项目angular-cli.

通过基本的路由设置,我现在想要从头部导航到一些路由,但由于我的头部是父的router-outlet,我收到此错误.

app.component.html

<app-header></app-header> // Trying to navigate from this component
    <router-outlet></router-outlet>
<app-footer></app-footer>
Run Code Online (Sandbox Code Playgroud)

header.component.html

  <a [routerLink]="['/signin']">Sign in</a>
Run Code Online (Sandbox Code Playgroud)

现在我部分理解我猜,因为该组件是一个包装器,router-outlet所以不可能通过这种方式访问router.那么,是否有可能从外部访问导航这样的场景?

如果需要,我会很乐意添加更多信息.先感谢您.

更新

1-我package.json已经有了稳定的@angular/router 3.3.1版本.2-在我的主app模块中,我已经导入了routing-module.请看下面.

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AlertModule  } from 'ng2-bootstrap';
import { LayoutModule } from './layout/layout.module';
import { UsersModule } from './users/users.module';
import { AppRoutingModule } from  './app-routing.module';
import { AppComponent } from './app.component';
import { PageNotFoundComponent } from './shared/components/not-found.component';

@NgModule({
  declarations: [
    AppComponent,
    PageNotFoundComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    AlertModule.forRoot(),
    LayoutModule,
    UsersModule,
    AppRoutingModule  --> This is the routing module. 
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

APP-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { SigninComponent } from './users/signin/signin.component';
import { PageNotFoundComponent } from './shared/components/not-found.component';

const routes: Routes = [
{ path: '**', component: PageNotFoundComponent }
];

@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})

export class AppRoutingModule {}
Run Code Online (Sandbox Code Playgroud)

我试图访问的路由是从另一个路由委托moduleUsersModule

用户routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { SigninComponent } from './signin/signin.component';

const usersRoutes: Routes = [
  { path: 'signin',  component: SigninComponent }
];
@NgModule({
  imports: [
    RouterModule.forChild(usersRoutes)
  ],
  exports: [
    RouterModule
  ]
})

export class UsersRoutingModule { }
Run Code Online (Sandbox Code Playgroud)

虽然我试图从作为Layout模块一部分的组件导航,但没有路由器模块的概念.这是导致错误的原因.

Layout.module.ts

import { NgModule } from '@angular/core';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './footer/footer.component';

@NgModule({
  declarations: [HeaderComponent, FooterComponent],
  exports: [HeaderComponent, FooterComponent]
})
export class LayoutModule{}
Run Code Online (Sandbox Code Playgroud)

我试图从中导航HeaderComponent.如果需要,我很乐意提供更多信息.

Gün*_*uer 157

您需要添加RouterModuleimports的每一个@NgModule()地方组件使用的任何部件或指令(在这种情况下routerLink<router-outlet>.

declarations: [] 是在当前模块内部制作组件,指令,管道.

exports: []是为组件,指令,管道,导入模块.添加的内容declarations仅对模块是私有的.exports让他们公开.

  • 感谢您的回答!就我而言,该应用程序可以运行,但测试失败。解决方案是在测试中为每个使用 routerLink 的组件导入 RouterTestingModule。 (4认同)

小智 17

您需要在主 module.ts 文件中导入 RouterModule

import {RouterModule} from '@angular/router';


@NgModule({
  imports: [RouterModule],
 )}
Run Code Online (Sandbox Code Playgroud)


Ben*_*rds 15

您缺少包含路由包,或者在主应用程序模块中包含路由器模块.

确保你的package.config有这个:

"@angular/router": "^3.3.1"
Run Code Online (Sandbox Code Playgroud)

然后在app.module中导入路由器并配置路由:

import { RouterModule } from '@angular/router';

imports: [
        RouterModule.forRoot([
            {path: '', component: DashboardComponent},
            {path: 'dashboard', component: DashboardComponent}
        ])
    ],
Run Code Online (Sandbox Code Playgroud)

更新:

将AppRoutingModule移动到导入中的第一位:

imports: [
    AppRoutingModule.
    BrowserModule,
    FormsModule,
    HttpModule,
    AlertModule.forRoot(), // What is this?
    LayoutModule,
    UsersModule
  ],
Run Code Online (Sandbox Code Playgroud)


cch*_*man 14

我将添加另一个案例,其中我遇到了相同的错误,但只是一个假人。我已添加[routerLinkActiveOptions]="{exact: true}"但尚未添加routerLinkActive="active".

我的错误代码是

<a class="nav-link active" routerLink="/dashboard" [routerLinkActiveOptions]="{exact: true}">
  Home
</a>
Run Code Online (Sandbox Code Playgroud)

应该是什么时候

<a class="nav-link active" routerLink="/dashboard" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">
  Home
</a>
Run Code Online (Sandbox Code Playgroud)

没有routerLinkActive,你就不能拥有routerLinkActiveOptions


小智 14

我收到此错误,即使我已从 app-routing.module 导出 RouterModule 并在根模块(app module)中导入 app-routingModule 。

然后我发现,我只在路由模块中导入了组件。

在我的根模块(应用程序模块)中声明组件可以解决问题。

declarations: [
AppComponent,
NavBarComponent,
HomeComponent,
LoginComponent],
Run Code Online (Sandbox Code Playgroud)


ahm*_*ira 13

RouterModule只需像这样添加:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { SidebarItemComponent } from './sidebar-item/sidebar-item.component';
import { RouterModule } from '@angular/router';

@NgModule({
  declarations: [
    SidebarItemComponent
  ],

  imports: [
    CommonModule,
    RouterModule,
  ], 
})
export class LayoutModule { }
Run Code Online (Sandbox Code Playgroud)


Tud*_*dor 8

当它应该工作时没有其他工作时,重新启动 ng serve。这是悲哀地发现这种虫子。

  • 可以确认存在错误。我只是不断删除 dist 文件夹的内容并重建。终于成功了。尝试添加指向 app.component.ts 中每个组件的链接。如果这不起作用,你唯一的选择就是祈祷和禁食。 (3认同)

the*_*hme 7

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",
Run Code Online (Sandbox Code Playgroud)

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: [...]
    });
  });
Run Code Online (Sandbox Code Playgroud)


小智 5

这个问题是因为你没有导入模块

import {RouterModule} from '@angular/router';
Run Code Online (Sandbox Code Playgroud)

并且您必须在导入部分声明此模块

   imports:[RouterModule]
Run Code Online (Sandbox Code Playgroud)