尝试使用angular-cli创建一个带有angular2的应用程序我面临以下问题: 我想配置导航和路由,但是我收到以下错误:
未处理的Promise拒绝:模板解析错误:无法绑定到 'routerLink',因为它不是'a'的已知属性。 ( “S =” 导航栏 navbar-fixed-top navbar-custom“>
] [routerLink] = “[ '/家']” >电价
App结构如下:
app/
core/
nav/
nav.component.ts|html|scss
core.module.ts
home/
home.component.ts|html|scss
app-routing.module.ts
app.component.ts|html|scss
app.module.ts
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 { AppRoutingModule } from './app-routing.module';
import { CoreModule } from './core/core.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
AppRoutingModule,
CoreModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
core.module.ts:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NavComponent } from './nav/nav.component';
@NgModule({
imports: [
CommonModule // we use ngFor
],
exports: [NavComponent],
declarations: [NavComponent],
})
export class CoreModule { }
应用-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'home',
component: HomeComponent
},
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: []
})
export class AppRoutingModule { }
nav.component.html
<nav class="navbar navbar-fixed-top navbar-custom">
<div class="container">
<a class="navbar-brand" [routerLink]="['/home']">FiT</a>
<button class="navbar-toggler hidden-md-up" type="button" data-toggle="collapse" data-target="#fit-nav" aria-controls="fit-nav" aria-expanded="false" aria-label="Toggle navigation">
☰
</button>
<div class="collapse navbar-toggleable-sm" id="fit-nav">
<ul class="nav navbar-nav pull-md-right">
<li class="nav-item">
<a class="nav-link">link</a>
</li>
<li class="nav-item">
<a class="nav-link">link</a>
</li>
<li class="nav-item">
<a class="nav-link">Log in</a>
</li>
<li class="nav-item">
<a class="nav-link">sign up</a>
</li>
</ul>
</div>
</div>
</nav>
所以我试图找到答案,我看到了许多类似的问题,但建议的解决方案没有帮助。
代码中的一切看起来都很好。也许我错过了什么?
答案 0 :(得分:3)
指令,组件和管道的范围限定为声明它们的模块,除非它导入导出这些项目的模块。例如
@NgModule({
declarations: [ SomeComponentThatUsesRouterLink ]
})
class SomeModule {}
@NgModule({
imports: [
SomeModule, <== SomeComponentThatUsesRouterLink can't use routerLink
RouterModule
],
declarations: [
AppComponent <== Can use routerLink
]
})
class AppModule {}
此处,即使AppModule
导入RouterModule
,其范围仅限于其自己的declarations
,而不是SharedModule
,因此{{ 1}}无法使用SomeComponentThatUsesRouterLink
指令。
现在看这里
routerLink
现在@NgModule({
exports: [ RouterModule, CommonModule, FormsModule ]
})
class SharedModule {}
@NgModule({
imports: [ SharedModule ],
declarations: [ SomeComponentThatUsesRouterLink ]
})
class SomeModule {}
可以使用SomeModule
指令,因为它导入了一个导出它的模块
或者,如果您希望RouteModule
可以导入SomeModule
本身
RouterModule
因此,要在特定情况下解决问题,您需要将@NgModule({
imports: [ RouterModule ],
declarations: [ SomeComponentThatUsesRouterLink ]
})
class SomeModule {}
导入RouterModule
,因为CoreModule
需要路由器指令
答案 1 :(得分:2)
您有语法错误
<a class="navbar-brand" routerLink="/home">FiT</a>