对于 angular 2 我需要将什么导入到我的组件中才能让 routerLink 在我的视图上工作

For angular 2 what do I need to have imported into my component for routerLink to work on my view

对于 angular 2 我需要将什么导入我的组件才能让 routerLink 在我的视图上工作?

我想我需要从我的@angular/router 导入一些东西?然后将其添加到我的组件指令中?

但我不确定那是什么。

您可以参考文档。有常量 - ROUTER_PROVIDERS and ROUTER_DIRECTIVES.

确保您要使用哪个路由器。在 Beta 版本之前,有一个版本是 deprecated in RC1.

你需要ROUTER_DIRECTIVES、ROUTER_PROVIDERS

RouteConfig 为导航配置路由和路由器

更新:@angular/router

的路线

来源:https://angular.io/docs/ts/latest/guide/router-deprecated.html

更新 不确定您是否需要! : https://i.imgsafe.org/55d36e1e91.png

main.ts

import { bootstrap } from '@angular/platform-browser-dynamic';

import { Component } from "@angular/core";
import { Routes, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from  '@angular/router';

import { Comp1 } from  './comp1';
import { Comp2 } from  './comp2';
import { Comp3 } from  './comp3';
import { Comp4 } from  './comp4';
import { Comp5 } from  './comp5';

@Component( {
    selector: 'root',
    template: `<h1>ROOT Component</h1>
    <a [routerLink]="['/comp1']" >Comp1 </a>
    <a [routerLink]="['/comp2']" >Comp2 </a>
    <a [routerLink]="['/comp3']" >Comp3 </a>
    <a [routerLink]="['/comp4']" >Comp4 </a>
    <a [routerLink]="['/comp5']" >Comp5 </a>

    <router-outlet></router-outlet>
    `,
    directives: [ ROUTER_DIRECTIVES ], providers: [ ROUTER_PROVIDERS ]

} )

@Routes( [  
    {path: '/comp1', component: Comp1 },
    {path: '/comp2', component: Comp2 },
    {path: '/comp3', component: Comp3 },
    {path: '/comp4', component: Comp4 },
    {path: '/comp5', component: Comp5 }

] )

export class Root  { }


bootstrap( Root );

compX.ts

import { Component } from "@angular/core";

@Component( {
    selector: 'comp1',
    template: `<h1>Child Component1</h1>
    `
} )

export class Comp1  { }