2016-12-27 56 views
-1

我已经检查了所有的教程,没有找到,我做错了什么。routerlink不能在angular2中工作

的AppModule:

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core'; 
import { FormsModule } from '@angular/forms'; 
import { HttpModule } from '@angular/http'; 

import { AppComponent } from './app.component'; 
import {BlogComponent} from './blog/blog.component'; 
import { PortfolioComponent } from './portfolio/portfolio.component'; 
import { NavbarComponent } from './shared/navbar/navbar.component'; 
import {TimelineComponent} from './timeline/timeline.component'; 
import {HomeComponent} from './home/home.component'; 

import { routing} from './app.routing'; 

@NgModule({ 
    declarations: [ 
    AppComponent, 
    PortfolioComponent, 
    NavbarComponent, 
    BlogComponent, 
    TimelineComponent, 
    HomeComponent 
    ], 
    imports: [ 
    BrowserModule, 
    FormsModule, 
    HttpModule, 
    routing 
    ], 
    providers: [], 
    bootstrap: [ 
    AppComponent 
    ] 
}) 
export class AppModule { } 
</i> 

Navigation Bar 
<i> 
<div id="navbar" class="collapse navbar-collapse"> 
     <ul class="nav navbar-nav"> 
     <li><a routerlink="/portfolio">Portfolio</a></li> 
     <li><a routerlink="/timeline">Timeline</a></li> 
     <li><a routerlink="/blog">Blog</a></li> 
     </ul> 
     <ul class="nav navbar-nav navbar-right"> 
     <li><a href="#contact">Professionals</a></li> 
     <li><a href="#contact">Students</a></li> 
     </ul> 
    </div> 
</i> 

NGModule : 
<i> 
import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core'; 
import { FormsModule } from '@angular/forms'; 
i 

import { routing} from './app.routing'; 

@NgModule({ 

    imports: [ 
    BrowserModule, 
    FormsModule, 
    HttpModule, 
    routing 
    ], 
    providers: [], 
    bootstrap: [ 
    AppComponent 
    ] 
}) 

请查看上面的代码,请ANF帮我找到为什么routerlink没有工作呢。

+1

有什么错误?你的路线在哪里? – Milad

回答

2

不确定您粘贴的代码是否正确排序,但我可以看到2个错误。

首先,您要设置routerlink属性而不是routerLink,请注意区分大小写。第二,我敢肯定你不会将模块中的RouterModule与相关组件导入,请确保你这样做,以便你首先访问routerLink指令。

2

您显示的代码是正确的。

我认为你的问题是你没有导入RouterModule(这是声明组件的地方)到使用这个模板的模块中。

声明与此模板组件添加模块中

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

然后将其添加到您的模块,例如进口

@NgModule({ 
    imports: [ 
    RouterModule 
    ], 
    declarations: [MyComponent] 
}) 
export class MyTemplatesModule { } 
0

与routerLink锚标签必须在同一个地方去标签路由器出口