Hulke
Hulke

Reputation: 857

Angular 2 proper way to template a [routerLink] in a child component

I am trying to find the way to template a [routerLink] in a child component.

In my app.component i have have the <router-outlet></router-outlet>.

Now, inside otherModule.component I am trying to apply a <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 { }

routes:

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 {

}

My main problem is that i need to route the root router and not the chilren routes

Upvotes: 0

Views: 951

Answers (1)

anshuVersatile
anshuVersatile

Reputation: 2068

Have you tried

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

or

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

Upvotes: 3

Related Questions