无法绑定到"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
.
通过基本的路由设置,我现在想导航到标头中的某些路由,但是由于我的标头是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.
推荐答案
您需要将RouterModule
添加到每个@NgModule()
的imports
中,其中组件使用任何组件或指令(在本例中为routerLink
和<router-outlet>
.
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: []
是为了使组件,指令,管道在当前模块内部已知.
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屋!