Angular 2在子组件中模板[routerLink]的正确方法

时间:2016-11-10 10:14:04

标签: javascript angular routes

我试图找到在子组件中模板[routerLink]的方法。

app.component我已经拥有<router-outlet></router-outlet>

现在,在otherModule.component内,我正在尝试应用<a [routerLink]="['home']">Home</a>

app.module:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
import {HomeModule} from "../home/home.module";
import {OtherModule} from "../otherModule/otherModule.module";
import {RouterModule} from "@angular/router";
import {routes} from "./routes";

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HomeModule,
    HttpModule,
    OtherModule,
    RouterModule.forRoot(routes)
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

路线:

import {HomeComponent} from "../login/login.component";
import {OtherComponent} from "../clickStudio/clickStudio.component";

export const routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'home', component: HomeComponent },
  { path: 'other', component: OtherComponent },

]

other.component:

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

@Component({
  selector: 'other',
  template: `<a [routerLink]="['home']">Home</a>`,
  styleUrls: ['./clickstudio-menu.component.scss']
})
export class OtherComponent {

}

我的主要问题是我需要路由 root 路由器而不是chilren路由

  • 实现此目的的最佳做法是什么?
  • 任何保持绑定到routerLink的方法吗?

1 个答案:

答案 0 :(得分:3)

你试过吗

<a routerLink="home">Details</a>

<a routerLink="/home">Details</a>