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

时间:2017-02-04 01:04:45

标签: angular typescript angular2-routing

最近,我开始玩角度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>

header.component.html

  <a [routerLink]="['/signin']">Sign in</a>

现在我部分理解我认为,由于该组件是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 { }

应用-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 {}

我尝试访问的路由是从module

的另一个UsersModule委派的

用户-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 { }

当我尝试从属于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{}

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

12 个答案:

答案 0 :(得分:102)

您需要将RouterModule添加到每个imports的{​​{1}},其中组件使用任何组件或指令(在这种情况下为@NgModule()routerLink。< / p>

<router-outlet>是在当前模块中创建组件,指令,管道。

declarations: []是使组件,指令,管道可用于导入模块。添加到exports: []的内容仅对模块是私有的。 declarations将其公开。

答案 1 :(得分:10)

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

确保您的package.config具有以下内容:

"@angular/router": "^3.3.1"

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

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

imports: [
        RouterModule.forRoot([
            {path: '', component: DashboardComponent},
            {path: 'dashboard', component: DashboardComponent}
        ])
    ],

<强>更新

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

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

答案 2 :(得分:6)

如果其他什么都不起作用,请重新启动ng serve。这是悲哀地发现这种虫子。

答案 3 :(得分:3)

您需要将RouterMoudle添加到包含imports组件的模块的Header部分中

答案 4 :(得分:2)

我将添加另一种情况,即我遇到相同的错误,但只是一个假人。我添加了 waitForKeyElements (".form-control:has(option[value=AL])", selectDropdown); function selectDropdown (jNode) { var evt = new Event ("click"); jNode[0].dispatchEvent (evt); jNode.val('AK'); evt = new Event ("change"); jNode[0].dispatchEvent (evt); } 却没有添加[routerLinkActiveOptions]="{exact: true}"

我的错误代码是

routerLinkActive="active"

应该在什么时候

<a class="nav-link active" routerLink="/dashboard" [routerLinkActiveOptions]="{exact: true}">
  Home
</a>

没有<a class="nav-link active" routerLink="/dashboard" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}"> Home </a> ,就没有routerLinkActive

答案 5 :(得分:2)

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

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

在我的根模块(App Module)中声明组件解决了这个问题。

declarations: [
AppComponent,
NavBarComponent,
HomeComponent,
LoginComponent],

答案 6 :(得分:2)

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

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

并且你必须在导入部分声明这个模块

   imports:[RouterModule]

答案 7 :(得分:1)

就我而言,我只需要将我新创建的组件导入到 RouterModule

{path: 'newPath', component: newComponent}

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

从'@ angular / router'导入{RouterModule};

imports: [
        RouterModule.forRoot([
            {path: '', component: DashboardComponent},
            {path: 'dashboard', component: DashboardComponent},
            {path: 'newPath', component: newComponent}
        ])
    ],

希望这对某些人有帮助!

答案 8 :(得分:0)

我正在为Angular应用程序运行测试,并且也遇到错误Can't bind to 'routerLink' since it isn't a known property of 'a'

我认为显示我的Angular依赖关系可能会有用:

    "@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",

问题出在我的spec文件中。我与另一个类似的组件spec文件进行了比较,发现在RouterTestingModule中缺少imports,例如

    TestBed.configureTestingModule({
      declarations: [
        ...
      ],
      imports: [ReactiveFormsModule, HttpClientTestingModule, RouterTestingModule],
      providers: [...]
    });
  });

答案 9 :(得分:0)

在当前组件的模块中,导入 RouterModule

赞:-

import {RouterModule} from '@angular/router';
@NgModule({
   declarations:[YourComponents],
   imports:[RouterModule]

...

它帮助了我。

答案 10 :(得分:0)

我完全选择了这种方法的另一种方式。

app.component.ts

import { Router } from '@angular/router';
export class AppComponent {

   constructor(
        private router: Router,
    ) {}

    routerComment() {
        this.router.navigateByUrl('/marketing/social/comment');
    }
}

app.component.html

<button (click)="routerComment()">Router Link </button>

答案 11 :(得分:0)

我刚失去关于这2小时。这是我的 Visual Studio 的一个错误。我不得不重新安装 Angular 并再次更新我的 NPM,现在它又可以工作了。

相关问题