无法绑定到“routerLink",因为它不是已知属性 [英] Can't bind to 'routerLink' since it isn't a known property
问题描述
最近,我开始玩 angular 2.到目前为止它很棒.所以,为了学习使用angular-cli
,我开始了一个演示个人项目.
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
.
通过基本的路由设置,我现在想从 header 导航到一些路由,但由于我的 header 是 router-outlet
的父级,我收到此错误.
With the basic routing setup, I now want to navigate to some routes from header, but since my header is a parent to the router-outlet
, I receive this error.
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
.那么,对于这样的场景,是否有可能从外部访问导航?
Now I understand partially I guess that since that component is a wrapper around router-outlet
it would not be possible this way to access router
. So, is there a possibility to access navigation from outside for a scenario like this?
如果需要,我很乐意添加更多信息.提前致谢.
I would be really happy to add any more information if needed. Thank you in advance.
更新
1- 我的 package.json
已经有了稳定的 @angular/router 3.3.1
版本.2- 在我的主 app
模块中,我导入了 routing-module
.请参阅下文.
1- My package.json
already has the stable @angular/router 3.3.1
version.
2- In my main app
module, I have imported the routing-module
. Please see below.
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 { }
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 {}
我尝试访问的路由是从另一个 module
委托的,即 UsersModule
The route I am trying to access is delegated from another module
that is the UsersModule
user-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
模块的组件进行导航,但没有路由器模块的概念.这就是导致错误的原因.
While I am trying to navigate from a component that is part of the Layout
module, but has no notion of the router module. Is that what is causing the error.
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
导航.如果需要,我很乐意提供更多信息.
I am trying to navigate from the HeaderComponent
. I would be happy to provide more information if needed.
推荐答案
你需要在每个@NgModule()
imports中添加RouterModule
> 其中组件使用来自的任何组件或指令(在本例中为 routerLink
和
.
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>
.
import {RouterModule} from '@angular/router';
@NgModule({
declarations:[YourComponents],
imports:[RouterModule]
declarations: []
是使组件、指令、管道在当前模块中已知.
declarations: []
is to make components, directives, pipes, known inside the current module.
exports: []
是使组件、指令、管道可用于导入模块.添加到 declarations
的内容仅对模块而言是私有的.exports
将它们公开.
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.
另见https://angular.io/api/router/RouterModule#usage-笔记
这篇关于无法绑定到“routerLink",因为它不是已知属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!